48

This is more specific and cleaner version of this question - Different DateTimeFormat for dev and test environment

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

Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");

When I set a breakpoint on Controller Action I see the following value of Thread.CurrentThread.CurrentCulture:

  1. In VS dev server - "en-GB"
  2. In IIS - "en-US"

Question is - What settings in IIS are responsible for this and how can I override it?

Community
  • 1
  • 1
dygo
  • 795
  • 1
  • 7
  • 17
  • possible duplicate of [Different DateTimeFormat for dev and test environment](http://stackoverflow.com/questions/7058111/different-datetimeformat-for-dev-and-test-environment) – H H Aug 27 '11 at 17:59
  • I can't immediately see a difference with your previous question. If there is, you should have linked to it and spelled out the difference. – H H Aug 27 '11 at 18:01
  • I'm new to StackOverflow so please advise me the appropriate way to do it. I've created this question because it's more specific and cleaner than the old one. I've set an update to the old question with a link to the current question. – dygo Aug 27 '11 at 18:17

4 Answers4

129

Rather than setting the Thread's culture, you can specify it in the web.config like so:

<configuration>
    <system.web>
        <globalization uiCulture="en-GB" culture="en-GB" />
    </system.web>
</configuration>

That is a more "proper" way of specifying the culture in ASP.NET.

vcsjones
  • 138,677
  • 31
  • 291
  • 286
  • @Traumer: in a "new and improved" question you should have mentioned that. Also a complete list of System and Browser language settings for all clients and servers would have been nice. – H H Aug 28 '11 at 19:50
  • Thanks for advice. I'll use it in future. But for this question I've already found a solution. – dygo Aug 28 '11 at 22:08
  • hi, thank you, adding web.config made work my date format, but is it possible to add globalization directly in particular view in mvc this way `@using System.Web.WebPages uiCulture="en-GB" culture="en-GB"`, but date format didn't work, it just shows as text in view, i found this idea [here](http://stackoverflow.com/a/8744037/2218697) – Shaiju T Mar 31 '15 at 09:02
19

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

Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");
dygo
  • 795
  • 1
  • 7
  • 17
6

I think it is a good option to just let the client (i.e. user agent / browser) decide what culture he wants. This can be done by setting the culture and uiCulture attribute of the globalization element in web.config to auto. See "Version 1".

You can also do something like: Take the broswers setting, but if not possbile use en-US as fallback value. See "Version 2".

Version 1:

<configuration>
   <system.web>    
      <globalization culture="auto" uiCulture="auto"/>
   </system.web>
</configuration>

Version 2:

<configuration>
   <system.web>    
       <globalization culture="auto:en-US" uiCulture="auto:en-US" />
   </system.web>
</configuration>


See also this article for more info: Auto Detecting and Setting ASP.NET Locale based on Browser Locale

Martin
  • 5,165
  • 1
  • 37
  • 50
0

To set a default Culture for your App in MVC, you can easily add this route in your RouteConfig class:

 foreach (var route in routes.Cast<Route>().Where(route =>
 route.GetType() == typeof(MultiLingualRoute)))
             {
                 route.Url = "{language}/" + route.Url;
                 route.Defaults.Add("language", "YOUR-DEFAULT");

             }
A.Dara
  • 784
  • 10
  • 23