ASP.NET MVC 2 - do UrlParameter.Optional entries have to be at the end of the route?
- by David M
I am migrating a site from ASP.NET MVC 1 to ASP.NET MVC 2. At the moment, the site supports the following routes:
/{country}/{language}/{controller}/{action}
/{country}/{controller}/{action}
/{language}/{controller}/{action}
/{controller}/{action}
The formats for country and language are distinguishable by Regex and have suitable constraints. In MVC 1, I registered each of these as a separate route - for each of around 20 combinations. In MVC 2, I have been trying to get the same thing working with one route to cover all four cases, using UrlParameter.Optional, but I can't seem to get it working - if I define country and language as both optional, then the route /Home/Index for example doesn't successfully match the route. This is what I am trying to do:
routes.MapRoute("Default",
"{country}/{language}/{controller}/{action}",
new { country = UrlParameter.Optional, language = UrlParameter.Optional,
controller = "Home", action = "Index" },
new { country = COUNTRY_REGEX, language = LANGUAGE_REGEX });
Is this just impossible because my optionals are at the beginning of the route, or am I just missing something? I can't find any documentation to either tell me what I am doing is impossible or to point me in the right direction.