Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Where to put RequestLocalization in the order? #15313

Closed
Ruard opened this issue Oct 23, 2019 · 1 comment
Closed

Where to put RequestLocalization in the order? #15313

Ruard opened this issue Oct 23, 2019 · 1 comment
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates

Comments

@Ruard
Copy link

Ruard commented Oct 23, 2019

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();
});
@javiercn javiercn added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Oct 23, 2019
@javiercn
Copy link
Member

@Ruard thanks for contacting us.

The routing middleware in endpoint routing only decides what route matches. The request continues to flow through the remaining middlewares and the route data is available after the routing middleware has run.

The right thing to do is to put UseLocalizalization after UseRouting.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 2, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates
Projects
None yet
Development

No branches or pull requests

2 participants