MapPageRoute 應該是 ASP.NET 4.0 中的東西,但現(xiàn)在我是第一次使用它,使用場景是:MVC 混合使用 WebForm,然后對 WebForm 進行路由配置,當然也可以使用 ISAPI_Rewrite 3,不同的是要在 IIS 中配置,相對而言,MapPageRoute 在程序中進行配置更加靈活方便,下面是關于 MapPageRoute 的簡單整理。
public class Mvcapplication : System.Web.HttpApplication{PRotected void Application_Start(){RouteTable.Routes.MapPageRoute("WebFormRoute","test","~/WebForm1.aspx");}}public partial class WebForm1 : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){Response.Write("hello world!");}}效果:

public class MvcApplication : System.Web.HttpApplication{protected void Application_Start(){RouteTable.Routes.MapPageRoute("WebFormRoute","test/{para}","~/WebForm1.aspx");}}public partial class WebForm1 : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){Response.Write("hello world! <br />");Response.Write("para: " + Page.RouteData.Values["para"]);}}效果:

注意:
當我們使用參數(shù)映射的時候,在獲取參數(shù)值使用的是 Page.RouteData.Values,而不是 Request.QueryString,有一種情況是,如果你的 Url 映射還是 WebForm1.aspx 文件名的形式,只不過這個文件地址發(fā)生了變化,這時候你就不需要在 MapPageRoute 中進行參數(shù)配置了,只需要進行 Url 和 WebForm 文件地址配置,那這樣配置,使用 Request.QueryString 可以獲取到參數(shù)值嗎?我試過是可以的,沒有了參數(shù)映射配置,也就不需要 Page.RouteData.Values 進行獲取參數(shù)值了,一種偷巧行為。
public class MvcApplication : System.Web.HttpApplication{protected void Application_Start(){RouteTable.Routes.MapPageRoute("WebFormRoute","test/{para1}/{para2}","~/WebForm1.aspx");}}public partial class WebForm1 : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){Response.Write("hello world! <br />");Response.Write("para1: " + Page.RouteData.Values["para1"]+ "<br />");Response.Write("para2: " + Page.RouteData.Values["para2"]+ "<br />");}}效果:

注意:
多個參數(shù)的映射還有一種寫法是:test/{para1}&{para2},但我試過這種寫法會報錯,而在 Scott Guthrie 的一篇博文中評論回復,這種方式是可以的,如下:

public class MvcApplication : System.Web.HttpApplication{protected void Application_Start(){RouteTable.Routes.MapPageRoute("WebFormRoute","test/{para1}/{para2}.html","~/WebForm1.aspx");}}public partial class WebForm1 : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){Response.Write("hello world! <br />");Response.Write("para1: " + Page.RouteData.Values["para1"]+ "<br />");Response.Write("para2: " + Page.RouteData.Values["para2"]+ "<br />");}}效果:

注意:
<system.webServer><modules runAllManagedModulesForAllRequests="true" /></system.webServer>在 MapPageRoute Url 映射的時候,使用的后綴是 .html,需要在 web.config 添加上面配置,如果使用的是 .aspx,則不需要,runAllManagedModulesForAllRequests 在之前的博文中有說過,就不重復了。
//參數(shù)public Route MapPageRoute(string routeName,//路由的名稱。string routeUrl,//路由的 URL 模式。string physicalFile,//路由的物理 URL。bool checkPhysicalUrlaccess,//一個值,該值指示 ASP.NET 是否應驗證用戶是否有權訪問物理 URL(始終會檢查路由 URL)。RouteValueDictionary defaults,//路由參數(shù)的默認值。RouteValueDictionary constraints,//一些約束,URL 請求必須滿足這些約束才能作為此路由處理。RouteValueDictionary dataTokens//與路由關聯(lián)的值,但這些值不用于確定路由是否匹配 URL 模式。)public class MvcApplication : System.Web.HttpApplication{protected void Application_Start(){RouteTable.Routes.MapPageRoute("WebFormRoute","test/{para1}/{para2}/{*queryvalues}","~/WebForm1.aspx",false,null,new RouteValueDictionary { { "para1", "^[0-9]*$" }, { "para2", "^[A-Za-z]+$" } },new RouteValueDictionary { { "para3", "xishuai1" }, { "para4", "xishuai2" } });}}public partial class WebForm1 : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){Response.Write("hello world! <br />");Response.Write("para1: " + Page.RouteData.Values["para1"]+ "<br />");Response.Write("para2: " + Page.RouteData.Values["para2"]+ "<br />");Response.Write("para3: " + Page.RouteData.DataTokens["para3"] + "<br />");Response.Write("para4: " + Page.RouteData.DataTokens["para4"]+ "<br />");}}效果:

參考資料:
新聞熱點
疑難解答