32

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?

Siegemos
  • 385
  • 1
  • 3
  • 10
  • 1
    Did you check the Internet Explorer Language Options? Web sites use these language settings to determine the current language – Jupaol Jun 21 '12 at 09:16
  • Yes, IE language is set to 'English (United Kingdom) [en-GB]'. I get the same issue in all browsers. – Siegemos Jun 21 '12 at 09:25
  • BTW how are you handling the resources? just RESX files under App_GlobalResources or App_LocalResources?? or are they embedded in satellite assemblies: – Jupaol Jun 21 '12 at 09:33
  • Also how do you determine the language in code, I mean do you keep the language in a cookie? or is it stored in a database for the current user perhaps? – Jupaol Jun 21 '12 at 09:35
  • App_LocalResources, but this particular problem is to do with the users entering a date and DateTime.Parse failing because it's expecting a US date, which it shouldn't be. I'm not specifically handling the language in code, I'm using Convert.ToDateTime on a string that has come from a calendar control. I'm not passing in a specific culture anywhere in code, I'm relying on the the site to pick up the culture from the user's browser settings. – Siegemos Jun 21 '12 at 10:08
  • It may be down to the fact that `CurrentUICulture` is always en-US for any version of English that `CurrentCulture` is set to. So, it may be that the curreny culture is being set from the UI Culture. Did you try what happens when you set a non-English language, especially a language with variants that display things like dates differently? – nicodemus13 Dec 02 '14 at 17:15

10 Answers10

28

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

Community
  • 1
  • 1
Jupaol
  • 21,107
  • 8
  • 68
  • 100
  • I've searched all of the code in the solution for 'en-US' and there are no relevant occurrences. The thing is, this is the same code that was running on the previous Windows 2003 server, and this was never a problem before so I'm thinking this is not a code issue but rather a server/IIS setting somewhere. Question is where? – Siegemos Jun 21 '12 at 09:36
  • If you change the language manually in the web.config of let's say 2 or 3 random app's only can you get the desire language?? If yes, you would be discarding deployment issues – Jupaol Jun 21 '12 at 09:37
  • Yes, if I add it to the web.config in the globalization section, specifically 'en-GB' then everything is fine but I'd like to avoid having to do that for all sites and get to the problem of where it is actually getting en-US from and change it at that level if possible. – Siegemos Jun 21 '12 at 09:55
  • Incidentally, I haven't used Web Deploy to deploy these sites. They were originally set up manually. – Siegemos Jun 21 '12 at 09:56
  • Machine.config - No globalization section, or mention of en-US Root web.config - No globalization section Website - No globalization section Web application - NA Folder - NA :(. – Siegemos Jun 21 '12 at 10:15
9

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".

SocratesG
  • 101
  • 1
  • 3
  • 1
    OMG man, that was the last change that i needed to make it work, i have changed everything... ¡EVERYTHING! So much thanks. I have been all the day with this... i think that i'm going to cry in happynes – ZeroCodeException Aug 01 '22 at 11:39
4

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.

Siegemos
  • 385
  • 1
  • 3
  • 10
3

.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. FF settings for Language

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();
        }
    }
Nirav Vyas
  • 131
  • 5
2

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

krlzlx
  • 5,752
  • 14
  • 47
  • 55
1

In my case, my application pool was running as my domain user, which had the current culture set to en-GB, and the application worked fine with en-GB date format.

I changed the app pool to be run under Network Service instead and suddenly the DateTime.Parse calls were breaking as the app was now using en-US culture. I saw some SO posts (e.g) about how IIS cultures are user-specific which explains that.

Opening the .NET Globalization of the root element in IIS Manager and setting both Culture and UI Culture to English UK (en-GB) fixed it for me.

demoncodemonkey
  • 11,730
  • 10
  • 61
  • 103
0

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;
} 
Caio
  • 1
0

I've spent hours trying to find a solution for my case. I am working under Win10 (English version), but with Russian regional settings. Nevertheless, I got "en-US" culture in my app, hosted on IIS with ApplicationPoolIdentity. That solved the problem:

IIS -> Application Pools -> Advanced Settings -> Change "Load User Profile" to False

irkForce
  • 352
  • 2
  • 12
0

I had this problem too, but I solved it. You must set the following values:

CultureInfo.DefaultThreadCurrentCulture = "en-GB";
CultureInfo.DefaultThreadCurrentUICulture = "en-GB";
0

I had this issue with a Windows Server Core 2022 installation so there's no simple UI solution like "Copy Settings" from the Regional Settings tab. I wanted a Powershell or CLI solution and like you say it can be applied for the user but not copied across to all users.

For that I found help from an article by Lewis Roberts on his blog https://www.lewisroberts.com/2017/03/01/set-language-culture-timezone-using-powershell/

You need to use control.exe to import a regional settings XML file. The following will be for en-GB UK region so adjust it to fit your requirements.

Save the following XML to a file called UKRegion.xml or whatever you want.

<gs:GlobalizationServices xmlns:gs="urn:longhornGlobalizationUnattend"> 
<!--User List-->
<gs:UserList>
    <gs:User UserID="Current" CopySettingsToDefaultUserAcct="true" CopySettingsToSystemAcct="true"/> 
</gs:UserList>
<!-- user locale -->
<gs:UserLocale> 
    <gs:Locale Name="en-GB" SetAsCurrent="true"/> 
</gs:UserLocale>
<!-- system locale -->
<gs:SystemLocale Name="en-GB"/>
<!-- GeoID -->
<gs:LocationPreferences> 
    <gs:GeoID Value="242"/> 
</gs:LocationPreferences>
<gs:MUILanguagePreferences>
    <gs:MUILanguage Value="en-GB"/>
    <gs:MUIFallback Value="en-US"/>
</gs:MUILanguagePreferences>
<!-- input preferences -->
<gs:InputPreferences>
    <!--en-GB-->
    <gs:InputLanguageID Action="add" ID="0809:00000809" Default="true"/> 
</gs:InputPreferences>
</gs:GlobalizationServices>

Then import it using the following command (Powershell formatted):

& $env:SystemRoot\System32\control.exe "intl.cpl,,/f:`"UKRegion.xml`""

You still need a reboot but this worked for me and set the ASP.NET workers to use en-GB locale by default without having to set it in code or in the web.config.

This was particularly useful for me as I am using it to set up EC2 instances using launch templates so many of these other solutions don't work.

Mike Smith
  • 574
  • 5
  • 6