2015-08-08 70 views
1

我有一个管理餐馆库存的网站。这是我的路线:与MVC中的不同路线冲突

routes.MapRoute(
    "Inventory",        
    "Inventory/{restaurantName}/{restaurantLocationId}/{code}", 
    new { controller = "Inventory", action = "Index" }, 
    new[] { "MySite.Web.Controllers" } 
); 

routes.MapRoute( // this route doesn't work 
    "ListRestaurantInventory", 
    "Inventory/List/{restaurantLocationId}/{code}", 
    new { controller = "Inventory", action = "ListRestaurantInventoryItems" }, 
    new[] { "MySite.Web.Controllers" } 
); 

routes.MapRoute(
    "InventoryDetails", 
    "Inventory/{restaurantName}/{restaurantLocationId}/{code}/Details/{restaurantInventoryItemId}", 
    new { controller = "Inventory", action = "Details" }, 
    new[] { "MySite.Web.Controllers" } 
); 

的问题是与ListRestaurantInventory路线,我得到一个404,如果我尝试导航到/Inventory/List/1/ABC。我的其他路线工作得很好。

我真的不知道我的路线有什么问题。我是否需要更改路线的顺序或URL中的参数?

回答

1

应该从最具体到最不具体的顺序列出路线。

Inventory路线覆盖您的ListRestaurantInventory因为与Inventory段开始,你通过与4段(如/Inventory/List/1/ABC)每个路由将匹配它。这基本上使您的ListRestaurantInventory路由不可达的执行路径。颠倒这两条路线的顺序将解决这个问题。

routes.MapRoute(
    "ListRestaurantInventory", 
    "Inventory/List/{restaurantLocationId}/{code}", 
    new { controller = "Inventory", action = "ListRestaurantInventoryItems" }, 
    new[] { "MySite.Web.Controllers" } 
); 

routes.MapRoute(
    "Inventory",        
    "Inventory/{restaurantName}/{restaurantLocationId}/{code}", 
    new { controller = "Inventory", action = "Index" }, 
    new[] { "MySite.Web.Controllers" } 
); 

routes.MapRoute(
    "InventoryDetails", 
    "Inventory/{restaurantName}/{restaurantLocationId}/{code}/Details/{restaurantInventoryItemId}", 
    new { controller = "Inventory", action = "Details" }, 
    new[] { "MySite.Web.Controllers" } 
);