Asp.net MVC 5 MapRoute for multiple routes

爱⌒轻易说出口 提交于 2021-02-09 10:56:38

问题


I have 3 routes in RouteConfig:

routes.MapRoute(
    name: "ByGroupName",
    url: "catalog/{categoryname}/{groupname}",
    defaults: new { controller = "Catalog", action = "Catalog" }
);
routes.MapRoute(
    name: "ByCatName",
    url: "catalog/{categoryname}",
    defaults: new { controller = "Catalog", action = "Catalog" }
);
routes.MapRoute(
    name: "ByBrandId",
    url: "catalog/brand/{brandId}",
    defaults: new { controller = "Catalog", action = "Catalog" }
);

and this is my action controller receiving parameters:

public ActionResult Catalog(
    string categoryName = null,
    string groupName = null,
    int pageNumber = 1,
    int orderBy = 5,
    int pageSize = 20,
    int brandId = 0,
    bool bundle = false,
    bool outlet = false,
    string query_r = null)
{
// ...
}

when I Use in view a link with @Url.RouteUrl("ByBrandId", new {brandId = 5}), I get in Action a parameter "categoryname"="brand" and brandId=0 instead of only brandId=5...

When I Call "http://localhost:3453/catalog/brand/5" with "ByBrandId" routeurl I want to get brandId=5 in actioncontroller..., the equivalent of "http://localhost:3453/catalog/Catalog?brandId=1"

thanks


回答1:


Your routing is misconfigured. If you pass the URL /Catalog/brand/something it will always match the ByGroupName route instead of the intended ByBrandId route.

First of all, you should correct the order. But also, the first 2 routes are exactly the same except for the optional group name, so you can simplify to:

routes.MapRoute(
    name: "ByBrandId",
    url: "catalog/brand/{brandId}",
    defaults: new { controller = "Catalog", action = "Catalog" }
);
routes.MapRoute(
    name: "ByGroupName",
    url: "catalog/{categoryname}/{groupname}",
    defaults: new { controller = "Catalog", action = "Catalog", groupname = UrlParameter.Optional }
);

Now when you use @Url.RouteUrl("ByBrandId", new {brandId = 5}) it should give you the expected output /catalog/brand/5.

See Why map special routes first before common routes in asp.net mvc for a complete explanation.



来源:https://stackoverflow.com/questions/37969797/asp-net-mvc-5-maproute-for-multiple-routes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!