我有一些URL,然后使用Webforms / Umbraco使其变得友好,而不是MVC。
www.mysite.com/p=1&c=2&d=3
到
www.mysite.com/1/2/3/ProductName
在UrlRewriting.config中添加一个友好的URL之后,我遇到的问题是,当使用友好的URL时,这些参数会同时出现,即1/2/3,因此使用
string pid = Server.HtmlDecode(Request.QueryString["p"]);
我得到的值返回为1/2/3
。我使用HTMLDecode,因为最初对值进行了编码(即,对斜线进行了编码)。
我知道我可以将它们放入数组/列表中并获得相关的值,但是我想知道是否应该有更好的方法或首选的方法?
答案 0 :(得分:0)
www.mysite.com/ProductName/1/2/3/
格式用于路由(与Web Api相同)。
但是您需要在RegisterRoutes(RouteCollection routes
文件中使用Global.asax.cs
。
小例子
void RegisterRoutes(RouteCollection routes)
{
//Example: https://my.syte.com/updatedns/qft
routes.MapPageRoute("updateDNS", "public/updateDNS/{hostname}",
"~/pages/public/updateDNS.aspx"
, false
);
}
如何添加Global.asax
您需要添加一个课程
在Global.asax
<%@ Application Language="C#" Inherits="Global" %>
在Global.asax.cs
中(针对您的情况)
public class Global : System.Web.HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RegisterRoutes(RouteTable.Routes);
}
/// Many other events like begin request...e.t.c
void RegisterRoutes(RouteCollection routes)
{
//Example: https://my.syte.com/updatedns/qft
routes.MapPageRoute("updateDNS", "public/updateDNS/{hostname}",
"~/pages/public/updateDNS.aspx"
, false
);
}
}