asp.netasp.net-mvcasp.net-mvc-5asp.net-mvc-routingmaproute

Asp.net MVC 5 MapRoute for multiple routes


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


Solution

  • 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.