【问题标题】:Conflict with different routes in MVC与 MVC 中的不同路由冲突
【发布时间】:2015-08-08 22:05:33
【问题描述】:

我有一个网站来管理餐馆的库存。这些是我的路线:

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 中参数的顺序?

【问题讨论】:

    标签: asp.net-mvc routes url-routing asp.net-mvc-routing


    【解决方案1】:

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

    您的Inventory 路由将覆盖您的ListRestaurantInventory,因为您通过的每条路由都以Inventory 分段开头的4 个分段(例如/Inventory/List/1/ABC)将匹配它。这实质上使您的 ListRestaurantInventory 路由成为无法访问的执行路径。颠倒这 2 条路线的顺序将解决此问题。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-19
      • 1970-01-01
      相关资源
      最近更新 更多