Question

I have a base controller in which I get and set the CurrentThread Culture.

    protected override void ExecuteCore()
    {
        string cultureName = null;
        // Attempt to read the culture cookie from Request
        HttpCookie cultureCookie = Request.Cookies["_culture"];
        if (cultureCookie != null)
            cultureName = cultureCookie.Value;
        else
            cultureName = Request.UserLanguages[0]; // obtain it from HTTP header AcceptLanguages

        // Validate culture name
        cultureName = CultureHelper.GetImplementedCulture(cultureName); // This is safe

        // Modify current thread's cultures            
        Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cultureName);
        Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

        base.ExecuteCore();
    }

Everything works fine. But now I realized that regarding SEO I will have some issues since probably not all my languages will be picked up.

So I thought about putting the CurrentCulture on the url :

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        name: "SpecificCulture",
        url: "{culture}/{controller}/{action}/{id}",
        defaults: new
        {
            controller = "Home",
            action = "Index",
            culture = Helpers.CultureHelper.GetCurrentNeutralCulture(),
            id = UrlParameter.Optional
        }
    );
}

However this doesn't work since my RegisterRoutes runs before my BaseController which means that the url does not refect the current selected culture, it does reflect the default culture.

So my question is : Is there anyway to update my routing maps after my culture is set?

Thanks

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top