title: ASP.Net MVC switching Cultures after compile for initial load
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Anyone have any ideas on this one? Bug in MVC?</p>
  
  <p>Edit (additional code and attempts):
  Even if I go completely overboard and include this in the base class of the view</p>
  
  <pre><code>public class DNViewPage&lt;T&gt; : ViewPage&lt;T&gt; 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();
          }
      }
  </code></pre>
  
  <p>and include this in the web.config</p>
  
  <pre><code>&lt;globalization requestEncoding="utf-8" responseEncoding="utf-8" uiCulture="en-US" culture="en-US"/&gt;
  </code></pre>
  
  <p>and this in the header of the .aspx file</p>
  
  <pre><code>&lt;%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Culture="en-US" UICulture="en-US"
  </code></pre>
  
  <p>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.</p>
  
  <p>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).</p>