MVC3 correct way to change culture for every request
Asked Answered
E

2

6

I am using MVC3, and have some logic for changing the culture which all works fine. My issue, is that there seems a few places where this change should be made, and I am unsure where would be the best place to do it.

Some examples show an override on each action, from within a controller like this:

protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
    // code to change culture
}

Whereas a more traditional way that I am used to seeing is doing it in the Global.asax file as follows:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    // code to change culture
}

What is the recommended place to do this?

Enter answered 22/2, 2012 at 14:34 Comment(0)
H
6

global.asax is the ASP.NET correct way of doing this. It works across frameworks (webforms, dynamic data,mvc).

Hultgren answered 22/2, 2012 at 14:36 Comment(2)
Is there a proper way to stop the culture code being called for requests to things like images, CSS files, JavaScript etcEnter
This in web.config: <system.webServer> <modules runAllManagedModulesForAllRequests="false"/> </system.webServer>Hultgren
H
4

Application_BeginRequest is fine. If you set the culture on the thread there, the whole HTTP Request will be executed in that culture.

Thread.CurrentThread.CurrentCulture = new CultureInfo(myCulture);
Higinbotham answered 22/2, 2012 at 14:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.