Search code examples
c#asp.net-mvcasp.net-mvc-routingasp.net-mvc-areas

ASP.NET MVC route doesn't work


I have 2 different routes:

context.MapRoute(
    "zyzzyva_default",
    "{urlTitle}",
    new { area = "zyzzyva", action = "Index", controller = "Home", urlTitle = UrlParameter.Optional }
);

and second:

context.MapRoute(
    "Vip_default_vip_thankyou",
    "{partnername}-vip-thank-you",
    new { controller = "Vip", action = "ThankYou", partnername = "" },
    new string[] { "Web.Areas.Vip.Controllers" }
);

When I go to mydomain.com/aaaa-vip-thank-you it should use the second route, but I don't understand why it uses the first route instead.


Solution

  • The first route is too general.

    Routing works with first match found in order they were registered.

    Change order of mapping.

    context.MapRoute(
        "Vip_default_vip_thankyou",
        "{partnername}-vip-thank-you",
        new { controller = "Vip", action = "ThankYou", partnername = "" },
        new string[] { "Web.Areas.Vip.Controllers" }
    );
    
    context.MapRoute(
        "zyzzyva_default",
        "{urlTitle}",
        new { area = "zyzzyva", action = "Index", controller = "Home",urlTitle = UrlParameter.Optional }
    );