Configuring ASP.NET MVC Attribute Routing for multi language site

Viewed 2563

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.

3 Answers
Related