ArsenAsulyan
ArsenAsulyan

Reputation: 49

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.

Upvotes: 4

Views: 621

Answers (1)

Nkosi
Nkosi

Reputation: 247098

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

Upvotes: 2

Related Questions