Asp.net MVC 5 MapRoute for multiple routes

Ssocial.it Web Agency picture Ssocial.it Web Agency · Jun 22, 2016 · Viewed 16.8k times · Source

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

Answer

NightOwl888 picture NightOwl888 · Jun 22, 2016

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.