2011-09-08 135 views
0

我想創建一個網站,動態映射在下面的時尚路線:MVC3 URL路由

http://domain/MyCategory1
http://domain/
http://domain/MyCategory1/MySubCategory

到目前爲止,我在給全球一個新的途徑增加。 ASAX

routes.MapRoute(
"IFAMainCategory", // Route name 
"{IFACategoryName}", // URL with parameters 
new { controller = "Home", action = "GetSubCategories", IFACategoryName=1} // Parameter defaults 
); 

但這則打亂了該標配缺省路由。

有什麼辦法可以控制這個嗎?

回答

2

你需要改變你的路線:

routes.MapRoute("MyCustomRoute", "MyCategory1/{action}/{id}", 
    new { controller = "MyCategory1", action = "MySubCategory", id = UrlParameter.Optional }); 

// Then the default route 

基本上,因爲你只是做一個巨大的航線捕手,所有路由匹配的那一個。如果要將特定路線映射到控制器,則需要具體說明。

0

您需要包括MyCategory1在路由名稱

routes.MapRoute("IFAMainCategory", 
// Route name "MyCategory1/{IFACategoryName}", 
// URL with parameters new { controller = "Home", action = "GetSubCategories", IFACategoryName=1} // Parameter defaults); 

看看這個其他職位,例如,並檢查了線路調試

.NET MVC custom routing

0

不幸的是,我不認爲你」重新實現你想要的東西。

你需要一些方法來路由分開,就像一個文件夾中放置你的「類別」:

routes.MapRoute(
       "IFAMainCategory", // Route name 
       "categories/{controller}/{action}/{id}", // URL with parameters 
       new { controller = "Home", action = "GetSubCategories", IFACategoryName=1 } 
      ); 

另一種選擇是,你可以對應用程序啓動默認路由之前每父類註冊路線:

routes.MapRoute(
       "IFAMainCategory 1", // Route name 
       "MyCategory1/{subcategory}", // URL with parameters 
       new { controller = "Home", action = "GetSubCategories", IFACategoryName=1, subcategory = UrlParameter.Optional } 
      ); 

routes.MapRoute(
       "IFAMainCategory 2", // Route name 
       "MyCategory2/{subcategory}", // URL with parameters 
       new { controller = "Home", action = "GetSubCategories", IFACategoryName=2, subcategory = UrlParameter.Optional } 
      );