title: ASP.NET Core 5 MVC localization query string working but routing not working
body: <p>In trying to localize my application, the query string localization has worked and accept language header also working, but routing is not working.
  Here is my code:</p>
  <p><strong>Startup.cs</strong></p>
  <pre class="lang-cs prettyprint-override"><code>public static IServiceCollection AddLocalizationServices(this IServiceCollection services, IConfiguration _config)
  {
      string defaultCulture = _config.GetValue&lt;string&gt;(&quot;DefaultCulture&quot;);
      services.AddLocalization(options =&gt; options.ResourcesPath = &quot;Resources&quot;);
      services.Configure&lt;RequestLocalizationOptions&gt;(
          options =&gt;
          {
              var supportedCultures = new[]
              {
                  new CultureInfo(&quot;en&quot;),
                  new CultureInfo(&quot;ar&quot;)
              };
  
              options.DefaultRequestCulture = new RequestCulture(culture: defaultCulture, uiCulture: defaultCulture);
              options.SupportedCultures = supportedCultures;
              options.SupportedUICultures = supportedCultures;
  
              options.RequestCultureProviders.Insert(0, new RouteDataRequestCultureProvider()
              {
                  Options = options,
                  RouteDataStringKey = &quot;lang&quot;,
                  UIRouteDataStringKey = &quot;lang&quot;
              });
              options.RequestCultureProviders.Insert(1, new QueryStringRequestCultureProvider());
              //options.RequestCultureProviders.Insert(2, new CookieRequestCultureProvider());
              options.RequestCultureProviders.Insert(3, new AcceptLanguageHeaderRequestCultureProvider());
          });
  
      services.Configure&lt;RouteOptions&gt;(options =&gt;
      {
          options.ConstraintMap.Add(&quot;lang&quot;, typeof(LanguageRouteConstraint));
      });
  
      return services;
  }
  
  public static IApplicationBuilder AddLocalizationBuilder(this IApplicationBuilder app)
  {
      var locOptions = app.ApplicationServices.GetService&lt;IOptions&lt;RequestLocalizationOptions&gt;&gt;();
      app.UseRequestLocalization(locOptions.Value);
  
      return app;
  }
  
  public void ConfigureServices(IServiceCollection services)
  {
      services.AddLocalizationServices(_config);
  
      services.AddControllersWithViews()
          .AddDataAnnotationsLocalization(options =&gt;
          {
              options.DataAnnotationLocalizerProvider = (type, factory) =&gt;
              {
                  var assemblyName = new AssemblyName(typeof(ValidationResource).GetTypeInfo().Assembly.FullName);
                  return factory.Create(&quot;ValidationResource&quot;, assemblyName.Name);
              };
          });
  }
  
  public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  {
      if (env.IsDevelopment())
      {
          app.UseDeveloperExceptionPage();
          //app.ConfigureExceptionHandler();
      }
      else
      {
          //Global error handling instead of try, catch
          app.ConfigureExceptionHandler();
  
          //app.UseHsts();
      }
  
      app.AddLocalizationBuilder();
  
      //app.UseHttpsRedirection();
  
      app.UseRouting();
  
      app.UseAuthorization();
  
      string defaultCulture = _config.GetValue&lt;string&gt;(&quot;DefaultCulture&quot;);
      app.UseEndpoints(endpoints =&gt;
      {
          endpoints.MapControllerRoute(
              name: &quot;default&quot;,
              pattern: &quot;{lang:lang}/{controller=Home}/{action=Index}/{id?}&quot;);
  
          endpoints.MapControllerRoute(
              name: &quot;default&quot;,
              pattern: &quot;{*catchall}&quot;,
              defaults: new { controller = &quot;Home&quot;, action = &quot;RedirectToDefaultLanguage&quot;, lang = defaultCulture });
      });
  }
  
  </code></pre>
  <p><strong>_ViewImports.cshtml</strong></p>
  <pre><code>@using CoreCMS.MVC
  @using CoreCMS.Data
  @using Microsoft.Extensions.Localization
  
  @inject IStringLocalizer&lt;SharedResource&gt; _loc
  
  @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
  </code></pre>
  <p><strong>Index.cshtml</strong></p>
  <pre><code>@_loc[&quot;Title&quot;]
  </code></pre>
  <p>When trying to access website with the following url : <strong>http://localhost:14896/en</strong> or <strong>http://localhost:14896/ar</strong> the culture not working
  but when using this url <strong>http://localhost:14896/?culture=ar</strong> or <strong>http://localhost:14896/?culture=en</strong> the culture is working well</p>