title: MVC Core set application culture from controller action
body: <p>I'm working on adding localization to my web application. I have configured the <code>IStringLocalizer</code> and it is correctly reading string resources from two different resx files, depending on the browser setting. It then maps those string resources to ViewData, from which my View is getting text in correct language (not sure if that is the best approach, but for now I don't want to spent more time on this). </p>
  
  <p>The thing is that I also have a drop down list in my UI, that allows users to manually switch language. I'm reading the value set by user in my controller action and adding it to cookies, but now I'd also like to set my applications' culture to the one matching the string in cookie.</p>
  
  <p>Is it possible to set application culture from the controller action in MVC Core? If yes, then how to do this correctly?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I have just learned that I can do something like this:</p>
  
  <pre><code>&lt;a class="nav-item nav-link" asp-route-culture="en-US"&gt;English&lt;/a&gt;
  </code></pre>
  
  <p>and it will add <code>?culture=en-US</code> to my route, which will set culture for the page for me. Is there any way to do the same without having to keep it in an address bar?</p>
  
  <p><strong>EDIT 2:</strong></p>
  
  <p>Regarding answer by Adam Simon: 
  <code>CookieRequestCultureProvider</code> is what I'd like to use in my app, but the problem is that it is not producing any cookie. Documentation says that .net core will resolve which provider to use by checking which will give a working solution, starting from <code>QueryStringRequestCultureProvider</code>, then going to <code>CookieRequestCultureProvider</code>, then other providers.</p>
  
  <p>My current Startup looks like this: </p>
  
  <pre><code>public class Startup
  {
      private const string defaultCulutreName = "en-US";
  
      public void ConfigureServices(IServiceCollection services)
      {
          services.AddLocalization(); 
          services.Configure&lt;RequestLocalizationOptions&gt;(options =&gt;
          {
              var supportedCultures = new[]
              {
                  new CultureInfo(defaultCulutreName),
                  new CultureInfo("pl-PL")
               };
              options.DefaultRequestCulture = new RequestCulture(defaultCulutreName, defaultCulutreName);
  
              options.SupportedCultures = supportedCultures;
              options.SupportedUICultures = supportedCultures;
          });
  
          services.AddMvc()
              .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix);
      }
  
      public void Configure(IApplicationBuilder app, IHostingEnvironment env)
      {
          if (env.IsDevelopment())
          {
              app.UseDeveloperExceptionPage();
          }
  
          app.UseStaticFiles();
  
          //TRIED PLACING IT BEFORE AND AFTER UseRequestLocalization
          //CookieRequestCultureProvider.MakeCookieValue(new RequestCulture("pl-PL", "pl-PL"));
  
          app.UseRequestLocalization(app.ApplicationServices
              .GetService&lt;IOptions&lt;RequestLocalizationOptions&gt;&gt;().Value);
  
          CookieRequestCultureProvider.MakeCookieValue(new RequestCulture("pl-PL", "pl-PL"));
  
          app.UseMvc(ConfigureRoutes);
      }
  
      private void ConfigureRoutes(IRouteBuilder routeBuilder)
      {
          routeBuilder.MapRoute("Default", "{controller=About}/{action=About}");
      }
  }
  </code></pre>
  
  <p>Regarding <code>CookieRequestCultureProvider.MakeCookieValue(new RequestCulture("pl-PL", "pl-PL"))</code> I have tried putting it in <code>RequestLocalizationOptions</code> in <code>ConfigureServices</code>, in <code>Configure</code> before <code>UseRequestLocalization</code> and after that. All with the same result.</p>
  
  <p>The following "problems" appear with this solution:</p>
  
  <ul>
  <li><p><code>MakeCookieValue</code> method is not producing any <code>.AspNetCore.Culture</code> cookie</p></li>
  <li><p>Chrome browser with language set to PL is using <code>pl-PL</code> culture
  correctly, yet Firefox is using <code>en-US</code> culture with language set to PL
  in options (despite commenting out <code>options.DefaultRequestCulture =
  new RequestCulture(defaultCulutreName, defaultCulutreName)</code> line)</p></li>
  <li><p>Somehow my localization is working by default without using query
  strings nor cookies to provide culture for application, but this is
  not how I'd like it to work, as I do not have any control over it</p></li>
  </ul>