Search code examples
asp.net-mvc-3routescustom-routes

MVC3 language route/constant overrides?


Given the following two routes defined:

routes.MapRoute(name: "StateResultsCategory", url: "{state}/{category}/{searchTerm}", defaults: new { controller = "Results", action = "SearchStateCategory" });

routes.MapRoute(name: "FRStateResults", url: "fr/{state}/{searchTerm}", defaults: new { controller = "Results", action = "SearchStateFR" });

The first route is trapping the "fr" and passing that off to the wrong action for "fr".

I don't want to set a constraint on the first route, as I may later have other language-specific routes. ie.

routes.MapRoute(name: "CHStateResults", url: "ch/{state}/{searchTerm}", defaults: new { controller = "Results", action = "SearchStateCH" });
routes.MapRoute(name: "SPStateResults", url: "sp/{state}/{searchTerm}", defaults: new { controller = "Results", action = "SearchStateSP" });

How do I setup the routes to accomodate this?

Thanks.


Solution

  • The order in which you place your routes makes a difference.

    Placing your more specific routes above the more generic routes ensures they won't get caught up in the more generic version.