title: How do I set the locale in asp.net core that my ModelBinders will use?
body: <p>I am trying to force my asp.net core 3.1 application to use the locale &quot;en-US&quot;, no matter the default locale of the Windows server it runs on.</p>
  <p>My main goal is to have ModelBinding correctly parse doubles entered by users. So far, I did not have any luck. The comma is interpreted as the decimal separator, whereas the point is interpreted as the thousands separator, which is consistent with the Windows default locale.</p>
  <p>So my code looks (simplified) like this:</p>
  <pre><code>public class Model
  {
      public double Percentage {get; set;}
  }
  
  [HttpPost]
  public ActionResult Index(Model model)
  {
      Debug.WriteLine(model.Percentage);
  }
  </code></pre>
  <p>This results in data entered and posted as <code>&quot;12.34&quot;</code> to be cast to double <code>1234</code>, etc.</p>
  <p>Previously, in ASP.Net MVC 5, adding this to the web.config file solved this issue:</p>
  <pre><code>  &lt;configuration&gt;
      &lt;system.web&gt;
        &lt;globalization culture=&quot;en-US&quot; uiCulture=&quot;en-US&quot; /&gt;
  </code></pre>
  <p>I have tried various methods described, which I will list below, but to no avail. These seem to be revolving around localization of the mark-up, but do not affect model binding.</p>
  <p>Apply the same settings as listed above.
  Adding this to the <code>Startup</code>'s <code>ConfigureServices()</code> method:</p>
  <pre><code>System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo(&quot;en-US&quot;);
  System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo(&quot;en-US&quot;);
  </code></pre>
  <p>Adding this to the same method:</p>
  <pre><code>services.Configure&lt;RequestLocalizationOptions&gt;(
      options =&gt;
      {
          options.DefaultRequestCulture = new RequestCulture(culture: &quot;en-US&quot;, uiCulture: &quot;en-US&quot;);
          options.SetDefaultCulture(&quot;en-US&quot;);
      });
  </code></pre>
  <p>The latter attempts being merely trial-and-error.</p>
  <p>I tried running on IIS and on IIS Express.</p>
  <p>I do realize I could write a custom model binder, but I think this adds unneeded complexity. Also, I could skip model binding and parse the post data manually, but the actual models are extensive and contain a lot of data. I do not want to go that way.</p>
  <hr />
  <p><strong>Addendum</strong></p>
  <p>I could reproduce this behavior with a small demo application. Steps to reproduce:</p>
  <p>Create a new web application in Visual Studio .Net 2019 with:</p>
  <ul>
  <li>ASP.Net Core Web Application</li>
  <li>Web Application (Model-View-Controller)</li>
  </ul>
  <p>Add a view model:</p>
  <pre><code>public class InputModel
  {
      [Range(0, 1)]
      public double Fraction { get; set; }
  }
  </code></pre>
  <p>Replace the index.cshtml with:</p>
  <pre><code>@model InputModel
  @using (Html.BeginForm(&quot;Index&quot;, &quot;Home&quot;, FormMethod.Post))
  {
      @Html.LabelFor(m=&gt;m.Fraction)
      @Html.TextBoxFor(m=&gt;m.Fraction)
      @Html.ValidationMessageFor(m =&gt; m.Fraction)
      &lt;input type=&quot;submit&quot; value=&quot;Submit&quot; /&gt;
  }
  </code></pre>
  <p>Add an action to the conntroller:</p>
  <pre><code>[HttpPost]
  public IActionResult Index(InputModel input)
  {
      if (ModelState.IsValid)
      {
          return Content($&quot;Received fraction: {input.Fraction:E3}&quot;);
      }
      return View(input);
  }
  </code></pre>
  <p>On my development Pc:</p>
  <ul>
  <li>input 0.5 is invalid and</li>
  <li>input 0,5 is valid.</li>
  <li>input 0,1 is valid, but the action will output:</li>
  </ul>
  <blockquote>
  <p>Received fraction: 1,000E+000</p>
  </blockquote>
  <p>Adding this to <code>ConfigureServices</code> in Startup.cs:</p>
  <pre><code>services.Configure&lt;RequestLocalizationOptions&gt;(
      options =&gt;
      {
          options.SupportedCultures = new List&lt;CultureInfo&gt; { new CultureInfo(&quot;en-US&quot;) };
          options.SupportedUICultures = new List&lt;CultureInfo&gt; { new CultureInfo(&quot;en-US&quot;) };
          options.SetDefaultCulture(&quot;en-US&quot;);
      });
  </code></pre>
  <p>does not make a difference.</p>
  <p>Only when I set change settings through Control Panel - Clock and Region - Change date time or number formats, can I make my web application accept the dot as the decimal separator. I do not like having to rely on that.</p>