Reputation: 937
I'm trying to set the current culture to invariant
Done this in my web.config
<globalization uiCulture="" culture="" />
Added this to my Application_Start()
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
But when i call the method on my controller the Thread.CurrentThread.CurrentCulture is set to da-DK
how can this be?
Upvotes: 0
Views: 5512
Reputation: 7514
You need to set the CurrentCulture
and CurrentUICulture
on the Request thread. This can be done by overriding the OnActionExecuted
or OnActionExecuting
methods either in the Controller or in an applied Action Filter:
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
}
Update: If you want to handle model binding scenarios you must forego the action filter:
public void Application_OnBeginRequest(object sender, EventArgs e)
{
var culture = GetCulture();
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
}
Upvotes: 0
Reputation: 1
As described at this question you need to use application_beginrequest if you want this to have any effect on the modelbinder.
Modelbinders are executed before the controller's action is called, because they create the values that are passed into the controller's action
Upvotes: 0
Reputation: 33149
You are only setting the culture on the current thread once the application starts. But a user request may be handled by another thread.
The solution, therefore, is to make sure that in the beginning of every request, you set the right culture on that thread.
In MVC 3, you may do that by setting the right culture in your Controller's OnActionExecuting()
method.
Upvotes: 1