Skip to content

Where to put RequestLocalization in the order? #15313

@ghost

Description

This issue is about the order of configuration.

string[] SupportedCultures = new[] { "en", "de", "fr", "nl" };

My configuration:

services.AddControllersWithViews()
    .SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
    .AddViewLocalization();

services.AddLocalization();

services.Configure<RequestLocalizationOptions>(options =>
{
    options.AddSupportedCultures(SupportedCultures)
        .AddSupportedUICultures(SupportedCultures)
        .SetDefaultCulture("en");

    options.AddInitialRequestCultureProvider(new RouteDataRequestCultureProvider());
});

The current documentation, which is not up-to-date BTW:

The localization middleware must be configured before any middleware which might check the request culture

app.UseRequestLocalization(new RequestLocalizationOptions { });
app.UseStaticFiles();
app.UseAuthentication();

But RouteDataRequestCultureProvider is ignored when I put UseRequestLocalization before StaticFiles (or actually before UseRouting):

app.UseRequestLocalization(); // <--
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();

And works only when put after UseRouting:

app.UseStaticFiles();
app.UseRouting();
app.UseRequestLocalization(); // <--
app.UseAuthentication();
app.UseAuthorization();

I don't see how I could use the RouteData for e.g. static files or custom middleware that comes after UseRequestLocalization and before UseRouting. How should this be solved?

Additional information:

var cultureString = string.Join("|", SupportedCultures);

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute("default", "{culture:regex(^(" + cultureString + ")$)}/{controller=Home}/{action=Index}/{id?}");
    endpoints.MapDefaultControllerRoute();
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templates

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions