2013-01-02 138 views
2

據我所知,在ASP.net 4.0 URL路由已合併到Web窗體中。這是偉大的,我可以做這樣的事情:ASP.net WebForms擴展名網址

void Application_Start(object sender, EventArgs e) 
{ 
    RegisterRoutes(RouteTable.Routes); 
} 
void RegisterRoutes(RouteCollection routes) 
{ 
    routes.MapPageRoute("Category", "list/{id}/{name}", "~/category.aspx"); 
    routes.MapPageRoute("Product", "item/{id}/{name}", "~/product.aspx"); 
} 

但有一個內置的方式爲我的路線擴展名的URL來自己的.aspx同行,而無需手動添加路由的每一個文件?例如:

account/login => account/login.aspx 
contact-us => contact-us.aspx 

感謝


我已經添加了以下爲我的路線一抓到底,所有的規則,這似乎是工作。

routes.MapPageRoute("Default", "{*file}", "~/{file}.aspx"); 

回答

4

您可以使用佔位符,就像在MVC

VB.NET

routes.MapPageRoute(
     "ThreeLevels", 
     "{folder}/{file}/{id}", 
     "~/{folder}/{file}.aspx", True, 
     New RouteValueDictionary From { 
      {"folder", "Home"}, 
      {"file", "Default"}, 
      {"id", Nothing} 
     }) 

C#

routes.MapPageRoute(
     "ThreeLevels", 
     "{folder}/{file}/{id}", 
     "~/{folder}/{file}.aspx", true, 
     new RouteValueDictionary { 
      {"folder", "Home"}, 
      {"file", "Default"}, 
      {"id", null} 
     });