I'm creating a MVC5 web site that should support multiple languages. The structure of the app is complex so I'm using Attribute Routing only. RouteConfig.cs is very simple:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
}
I want to keep language information in URL by adding language identifier after site name. For English, which is default language, URL should remain "clean". Here are an example:
http://test.com/foo/1/bar/2
http://test.com/de/foo/1/bar/2
My first attempt was to use two RoutePrefix attributes for each controller:
[RoutePrefix("foo")]
[RoutePrefix("{lang}/foo")]
But MVC doesn't allow to use more than one RoutePrefix attribute for a controller.
So now I'm not using RoutePrefix attributes at all, and specify full route for each controller action:
[Route("foo/{a}/bar/{b}")]
[Route("{lang}/foo/{a}/bar/{b}")]
Is there any better way to handle lang route? Ideally I would like to specify in one place only, not for every controller.
PS. I'm setting current thread culture by parsing language route in custom filter.
MapMvcAttributeRoutesand make a similar method that registers a localized route for eachRouteattribute (be sure to call your custom method beforeMapMvcAttributeRoutesto put the routes into the RouteTable in the right order).MapMvcAttributeRoutes()is called to create a copy of routes, then first route is of typeRouteCollectionRoutewhich can't be casted toRoute. And even if I ignore that, these added localized routes are not working.