开发者

Setting Culture for ASP.NET MVC application on VS dev server and IIS

This is more specific and cleaner version of this questi开发者_如何学运维on - 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?


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.


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");


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


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");

             }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜