0

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?

mimo
  • 937
  • 12
  • 19

3 Answers3

1

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.

Roy Dictus
  • 32,551
  • 8
  • 60
  • 76
0

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

Community
  • 1
  • 1
Jauco
  • 1
0

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;
}
Phil Klein
  • 7,344
  • 3
  • 30
  • 33
  • You would think. See the answers to this question for more information: http://stackoverflow.com/questions/1100061/how-to-configure-invariant-culture-in-asp-net-globalization – Phil Klein Dec 12 '11 at 16:48
  • Stupid that i have to denote all my methods with an custom action filter to get this to work.. can it not be done on a global scale? – mimo Dec 12 '11 at 17:04
  • 1
    Absolutely. Assuming you're using MVC 3 you can configure a Global Action Filter. See the following link: http://odetocode.com/Blogs/scott/archive/2011/01/15/configurable-global-action-filters-for-asp-net-mvc.aspx – Phil Klein Dec 12 '11 at 17:16
  • Thank you for that link. And for the help :-) – mimo Dec 13 '11 at 05:52
  • This doesn't work for modelbinding! see my answer further down. – Jauco Apr 09 '13 at 08:11