2

I'm having some problems with CultureInfo values for a specific culture, "pt-PT". To narrow it down I created an MVC project in VS 2017 using .net framework and added the following line to the view About.cshtml:

<p>@(new System.Globalization.CultureInfo("pt-PT").DateTimeFormat.FirstDayOfWeek)</p>

This was the only change I made to the project created by visual studio.

When I run this project in VS using the IIS Express, the value returned by FirstDayOfWeek is 'Monday'.

However if I run this project in IIS (on the same machine), the value returned by FirstDayOfWeek is 'Sunday'.

I was expecting the value to be the same and to be 'Monday'. I'm puzzled about the diference in values and would like some help in understanding it.

Note: After further tests I concluded that if I change the first day of the week in my computer settings, that change is reflected when running the project in IIS Express. I'm more confused... I guess this property is useless...

Gnomo
  • 407
  • 4
  • 13
  • 1
    Could it be that IIS Express uses the Location settings from your user profile and IIS uses the Location settings from the default user? – Stephen Sep 21 '17 at 12:03

1 Answers1

1

By default, IIS uses DateTimeInfo.InvariantInfo which returns Sunday as its FirstDayOfWeek value (also using United States date format too). If you're unsure why IIS (not Express) uses different culture than you're using, set the culture information in Application_BeginRequest handler inside Global.asax code:

protected void Application_BeginRequest(object sender, EventArgs e)
{
     string culture = "pt";
     if (HttpContext.Current.Request.Path.Contains("pt"))
     {
         culture = "pt"; // Portuguese, use 'pt-PT' if not sure
     }
     else
     {
         // set to other cultures, including invariant (default) one
     }

     Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(culture);
     Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture);
}

Or use globalization element in web.config file to set Thread.Culture & Thread.UICulture:

<globalization culture="pt-PT" uiCulture="pt" />

If all of the above settings doesn't work, use these steps below:

  1. Open IIS Manager, select your site and open ".NET Globalization".
  2. Open "Culture" tab, set both Culture & UI Culture section to Portuguese.
  3. Restart the application pool or use iisreset command to apply all changes.

NB: As a general convention, the site shouldn't rely on server's application pool setup to work as expected with specified locale/culture. See Set the Culture and UI Culture for ASP.NET Web Page Globalization for further details about culture settings.

Also check if the regional settings of your application pool set to follow NetworkService's user account, change it to LocalSystem if necessary.

Similar issues:

ASP.NET Application is displaying american date formats

How do you set the IIS Application Pool Identity User Locale when it's set to ApplicationPoolIdentity

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
  • I believe you are answering to a slightly different question. My question is that an object of type CultureInfo when created as `new System.Globalization.CultureInfo("pt-PT")` as different property values when running in IIS and in IIS Express. I have experimented your suggestions and the behaviour remais the same. – Gnomo Sep 21 '17 at 14:52