当前位置: 动力学知识库 > 问答 > 编程问答 >

globalization - CurrentCulture incorrectly defaulting to en-US in ASP.net

问题描述:

I have just migrated around 100 ASP.net sites from IIS 6 on Windows Sever 2003 to IIS 7 on Windows 2008. I've just noticed that various pieces of code that use things like DateTime.Parse have started kicking up errors "String was not recognized as a valid DateTime". I've tracked this down to the fact that the CurrentCulture of the sites is defaulting to 'en-US' and so my UK users are inputting dates in an unexpected format.

Question is, where are they getting en-US from? Starting from the top, if I look in 'Control Panel > Region and Language' everything is set to English (United Kingdom). The web.configs of the sites either don't have a <globalization> section or have it set as <globalization culture="auto" uiCulture="auto" />. In 'IIS7 - .Net Globalization' all of the sites have their culture set to 'Invariant Language (Invariant Country)'.

I can't find anywhere that's settings the culture to 'en-US'... but something is.

Thread.CurrentThread.CurrentCulture.Name is outputting 'en-US'

Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencySymbol is outputting '$'

I can fix the issue by adding <globalization culture="en-GB" uiCulture="en-GB" /> to every web.config BUT I really don't want to have to hand edit about 100 web.configs! I wan't it to inherit the culture from the server OS settings, which are set to en-GB.

Am I missing something?

网友答案:

These are alternative places where you could search:

I can't find anywhere that's settings the culture to 'en-US'... but something is.

Thread.CurrentThread.CurrentCulture.Name is outputting 'en-US' Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencySymbol is outputting '$'

Try looking for the InitializeCulture method, this method is overridden in ASP.Net pages to set the Culture like:

protected override void InitializeCulture()
{
    var hidden = this.Request.Form["hidden"];
    var culture = this.Request.Form[hidden];
    if (!string.IsNullOrWhiteSpace(culture))
    {
        this.Culture = culture;
        this.UICulture = culture;
    }

    base.InitializeCulture();
}

Try to look for the following assembly attributes:

    [assembly: AssemblyCulture("en-US")]
    [assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.MainAssembly)]

Try to look for the following page directive attributes:

    <%@ Page Culture="en-US" UICulture="en-US" Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

Try to look in web.configs:

<globalization uiCulture="en-US" culture="en-US" enableClientBasedCulture="false" />

Edit 1

Try to look for HttpHandlers or HttpModules trying to set the language

Try to look in the web.config hierarchy (at the server, <wwwroot> means the root folder of your IIS Web Site)

  1. Global machine. <windir>\Microsoft.NET\Framework\<ver>\Config\Machine.config
  2. Root Web config. <windir>\Microsoft.NET\Framework\<ver>\Config\Web.config
  3. Website. <wwwroot>\Web.config
  4. Web application. <wwwroot>\<webapp>\Web.config
  5. Folder. <wwwroot>\<webapp>\<dir>\Web.config

If you have multiple servers (web farm), check you are being redirected to the correct server (the one you are checking the configuration), in order to do it you can use the ip of the desired server or configure your host files in your client computer

网友答案:

If I add a globalization section in the root web.config ( windir\Microsoft.NET\Framework\ver\Config\Web.config), set to en-GB, it does solve my problem and propagates down to the other sites. Which kinda solves my problem. Still doesn't explain where its getting en-US from by default though but it should do the trick. Thanks.

网友答案:

It can come from the culture of a system account used to run the Application Pool or Web Application.

Check this link: different culture settings between IIS and ASP.NET

网友答案:

.Net web application is picking up your browser's default culture. For e.g. in FF, default language is set as shown in below image.

So, if you want your site's culture other than the one from browser, in page's InitializeCulture method (Create a BasePage and keep below code here and inherit existing pages from this BasePage).

protected override void InitializeCulture()
    {
            System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo("en-GB");
        System.Threading.Thread.CurrentThread.CurrentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture;

            base.InitializeCulture();
        }
    }
网友答案:

I had the same problem and after many hours I found out that even though the regional settings were correct, I also needed to change the original culture for all the reserved accounts (e.g. ASP.NET).

This is done through the button "Copy Settings..." under the Administrative tab in Regional Settings. The settings are copied if you enable the checkbox "Welcome screen and system accounts".

网友答案:

Just to help someone gets the same problem....

After tried change IIS Culture, set globalization and so far unsecessfully, i did it in Global.asax:

void Application_BeginRequest(Object sender, EventArgs e)
{
    System.Globalization.CultureInfo newCulture = (System.Globalization.CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();

    newCulture.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
    newCulture.DateTimeFormat.DateSeparator = "/";

    System.Threading.Thread.CurrentThread.CurrentCulture = newCulture;
} 
网友答案:

I am facing similar problem in my new MVC application, I tried to set global level say .netframwork /ver/web.config and application level web.config but still no use.

I am still getting en-US culture when convert a string to Datetime, however I am expecting it to be en-GB

Convert.ToDateTime(Request.QueryString["deptDate"]).Date

Strange thing same project which is in webform works fine where culture comes as en-GB. but not sure where en-US is picking up in MVC project. tried all options but taking lot of time to understand. May I get advise please. I dont think too much code change is required it is just that en-US is coming defaulted somewhere which im not able to figure it out.

分享给朋友:
您可能感兴趣的文章:
随机阅读: