4

In the Application_BeginRequest() method of global.asax.cs in my ASP.NET MVC project there is code:

Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(EnCultureKey);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(EnCultureKey);
Thread.CurrentThread.CurrentCulture.DateTimeFormat = new CultureInfo(EnGBCultureKey).DateTimeFormat;

The variables are

private const string EnCultureKey = "en-US";
private const string EnGBCultureKey = "en-GB";

On the dev environment all the dates are in DD/MM/YYYY format, but on the test environment they are in MM/DD/YYYY format.

Could You please advise me on what could be the cause of this difference?

UPDATE:

Please take a look at Setting Culture for ASP.NET MVC application on VS dev server and IIS

Community
  • 1
  • 1
dygo
  • 795
  • 1
  • 7
  • 17
  • How are you formatting the DateTimes? – Oded Aug 14 '11 at 16:34
  • Have you tried to set UICulture to en-GB. Does it format in the right way then? If so, you could change the third line to use the other culture. Notice: You are doing strange stuff! First you assign a culture and then you override its behavior. Doesn't make a lot of sense to me! – Alexander Schmidt Aug 14 '11 at 17:01

4 Answers4

4

If you do want to override these settings for all your pages (instead of giving the User a choice) then the standard way is a setting in web.config :

  <globalization uiCulture="en" culture="en-GB" />

The MSDN page also points you to overriding InitializeCulture() if you want to use code.

InitializeCulture() happens early but I suspect that Application_BeginRequest happens even earlier and that its effects are overridden.

H H
  • 263,252
  • 30
  • 330
  • 514
0

Try using that code on the Application_Start method of the global.asax, that will ensure that every time you start your application, the culture info is set to your specifications.

Make sure you're using the right format time, for example, to show the date:

DateTime dt = DateTime.Now; Console.WriteLine(dt.ToString("d"));

Luis
  • 5,786
  • 8
  • 43
  • 62
  • 1. I'll try Application_Start, but Application_BeginRequest must be sufficient too. 2. I'm not formatting my DateTime. I presume that it's using G - general format pattern by default. – dygo Aug 14 '11 at 17:29
  • Application Start happens only once. You _must_ do this per request. – H H Aug 14 '11 at 18:03
0

Ah I see, sorry.

I think it is because of course in this way you are changing the culture of a single thread, not all thread of the application.

Put your code in a place that is executed by every thread in the application, for example, the page load.

Salvatore Previti
  • 8,956
  • 31
  • 37
  • I'm trying to have the same datetime formatting on both dev and test environment. The question is why the same code leads to different behaviour. – dygo Aug 14 '11 at 18:33
0

Well, I didn't actually find what IIS setting is responsible, but I've overridden it in Application_PreRequestHandlerExecute() and it finally worked:

var culture = CultureInfo.CreateSpecificCulture(EnCultureKey);
culture.DateTimeFormat = CultureInfo.CreateSpecificCulture(EnGBCultureKey).DateTimeFormat;
Thread.CurrentThread.CurrentCulture = culture;

culture = new CultureInfo(EnCultureKey);
culture.DateTimeFormat = Thread.CurrentThread.CurrentCulture.DateTimeFormat;
Thread.CurrentThread.CurrentUICulture = culture;
dygo
  • 795
  • 1
  • 7
  • 17