Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC3 correct way to change culture for every request

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?

like image 938
eyeballpaul Avatar asked Feb 22 '12 14:02

eyeballpaul


2 Answers

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

like image 87
linkerro Avatar answered Oct 17 '22 05:10

linkerro


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);
like image 33
Roy Dictus Avatar answered Oct 17 '22 05:10

Roy Dictus