2015-08-08 59 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" } 
);