I have a hybrid ASP.Net web forms/MVC app. On one of the MVC "pages"/views, I have it render a bunch of dates using the ToShortDateString() and ToLongDateString(). These work correctly most of the time, but the first time I load the view after compiling the app, they are formatted incorrectly.
I traced this down and checked the current thread's culture. For 99% of the time it's en-US, but on the first load of the MVC view after compiling it is set to en-GB. If I reload the page immediately after that, it's back to en-US.
I have tried setting the culture and uiculture in the web.config file to en-US to force it to be correct, but no luck.
Anyone have any ideas on this one? Bug in MVC?
Edit (additional code and attempts): Even if I go completely overboard and include this in the base class of the view
public class DNViewPage<T> : ViewPage<T> where T : class
{
protected override void OnInit(EventArgs e) {
base.OnInit(e);
CultureInfo cultureInfo = new CultureInfo("en-US");
this.Culture = "en-US";
this.UICulture = "en-US";
Thread.CurrentThread.CurrentUICulture = cultureInfo;
Thread.CurrentThread.CurrentCulture = cultureInfo;
}
protected void Page_Load(object sender, EventArgs e) {
CultureInfo cultureInfo = new CultureInfo("en-US");
this.Culture = "en-US";
this.UICulture = "en-US";
Thread.CurrentThread.CurrentUICulture = cultureInfo;
Thread.CurrentThread.CurrentCulture = cultureInfo;
}
protected override void InitializeCulture() {
CultureInfo cultureInfo = new CultureInfo("en-US");
this.Culture = "en-US";
this.UICulture = "en-US";
Thread.CurrentThread.CurrentUICulture = cultureInfo;
Thread.CurrentThread.CurrentCulture = cultureInfo;
base.InitializeCulture();
}
}
and include this in the web.config
<globalization requestEncoding="utf-8" responseEncoding="utf-8" uiCulture="en-US" culture="en-US"/>
and this in the header of the .aspx file
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Culture="en-US" UICulture="en-US"
Again, this is only on the initial load after compiling the code, when that page first loads. Other web forms pages are unaffected, even if they descend from System.Web.Mvc.ViewPage. All subsequent loads treat the culture correctly. Just changing the .aspx file doesn't cause this, the c# code has to be compiled to cause this.
More data: I have it tracked down to the Render method. Before the Render method, the culture is en-US and afterwards it is en-GB (again only on initial pageload after compilation).