[英]URL Routing in ASP.net 4
我在Global.asax頁面中將此代碼用於asp.net 4中的“地圖頁面路線”
protected void RegistreRoutes(System.Web.Routing.RouteCollection routes)
{
routes.MapPageRoute(
"Lerning-browse", "Learning-CSharp", "~/CSharp.aspx");
}
protected void Application_Start(object sender, EventArgs e)
{
RegistreRoutes(System.Web.Routing.RouteTable.Routes);
}
當用戶在URL中輸入mysite.com/Learning-CSharp時。 MapPageRoute的工作方式並獲取mysite.com/Learning-CSharp URL。 但是,如果用戶在URL中輸入mysite.com/CSharp.aspx可以正常工作,並在URL中獲取mysite.com/CSharp.aspx 。
我不想在URL中獲取mysite.com/CSharp.aspx 。 我想如果用戶在URL中輸入mysite.com/CSharp.aspx ,將其轉換為mysite.com/Learning-CSharp
protected void RegistreRoutes(System.Web.Routing.RouteCollection routes)
{
routes.Ignore("{*allaspx}", new { allaspx = @".*\.aspx(/.*)?" });
routes.Ignore("{*allcss}", new { allcss = @".*\.css(/.*)?" });
routes.Ignore("{*alljpg}", new { alljpg = @".*\.jpg(/.*)?" });
routes.Ignore("{*alljs}", new { alljs = @".*\.js(/.*)?" });
routes.Add(new System.Web.Routing.Route("{resource}.css/{*pathInfo}", new
System.Web.Routing.StopRoutingHandler()));
routes.Add(new System.Web.Routing.Route("{resource}.js/{*pathInfo}", new
System.Web.Routing.StopRoutingHandler()));
routes.MapPageRoute(
"HomeRoute",
"",
"~/default.aspx"
);
routes.MapPageRoute(
"Lerning-browse", "Learning-CSharp", "~/CSharp.aspx");
}
protected void Application_Start(object sender, EventArgs e)
{
RegistreRoutes(System.Web.Routing.RouteTable.Routes);
}
這是IIS URL重寫的工作。
應該遵循以下方法:
<rewrite>
<rules>
<rule name="Rewrite to article.aspx">
<match url="(.*)(\.aspx)$" />
<action type="Rewrite" url="{R:1}" />
</rule>
</rules>
</rewrite>
如果您還想支持查詢字符串,則需要進一步修改。
如果您只需要管理一個或兩個路由,一個簡單的過程就是將它們作為硬編碼重定向簡單地添加到您的web.config文件中。
就像是:
<location path="CSharp.aspx">
<system.webServer>
<httpRedirect enabled="true" destination="/Learning-CSharp" exactDestination="true" httpResponseStatus="Permanent" />
</system.webServer>
</location>
我在許多落后的網站上都使用過這種重定向,尤其是在遷移到新的代碼庫時; 即從Web表單遷移到MVC。
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.