我有一个管理餐馆库存的网站。这些是我的路线:
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
路线,如果我尝试导航到/Inventory/List/1/ABC
,我会得到404。我的其他路线工作得很好。
我真的不知道我的路线有什么问题。我是否需要更改路由的顺序或URL中的参数的顺序?
答案 0 :(得分:2)
路线应按照从最具体到最不具体的顺序列出。
您的Inventory
路由覆盖了ListRestaurantInventory
,因为您通过4个段(例如/Inventory/List/1/ABC
)的Inventory
段开头的每条路线都会与之匹配。这实质上使您的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" }
);