Search code examples
asp.net-mvc-5http-headersglobalization

ASP.NET MVC (Content-language http header)


I'm developing content site (internationalised) using ASP.NET MVC. I use web.config (not clientbrowser setttings) to deliver region specific content.

<globalization culture="fr" uiCulture="fr" enableClientBasedCulture="false" />

I don't see ASP.net MVC framework is appending "Content-language" header automatically, is there a way to do that, and if yes than how. And if now than how can we put customised code most efficiently.

Regards.


Solution

  • In your controller, add:

    Response.AddHeader("Content-language",
        Thread.CurrentThread.CurrentUICulture.Name);
    

    you can apply it to all actions by creating a base class for all your controllers, and including this in overridden OnActionExecuting. Such as:

    public class MyController : BaseController
    {
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            Response.AddHeader("Content-language",
                Thread.CurrentThread.CurrentUICulture.Name);
            base.OnActionExecuting(filterContext);
        }
    }
    

    Your controllers should then be changed to use MyController instead of BaseController as their base class.