简体   繁体   中英

How to handle NullProviderCultureResult? Isn't it strange to render the homepage without redirection?

In a Net 7 project I am setting localization in Program :

builder.Services.Configure<RequestLocalizationOptions>(x => {
  x.AddSupportedCultures("pt", "en");
  x.AddSupportedUICultures("pt", "en");
  x.SetDefaultCulture("pt");
  x.RequestCultureProviders.Clear();
  x.AddInitialRequestCultureProvider(new RouteDataRequestCultureProvider { RouteDataStringKey = "culture" });
});

application.UseRequestLocalization();

I have two Razor Pages (Index and About):

Index: @page "{culture?}"

About: @page "/{culture?}/about"

I am able to access Homepage (Index) using:

/
/en
/pt

But I am only able to access About page using:

/en/about
/pt/about

When I try to access "/about" the url is not changed but the homepage is rendered.

Isn't this strange?

Shouldn't redirect to Index and change the Index Url?

Or even Redirect to "/pt/about" because PT is the default culture?

I checked the RouteDataRequestCultureProvider source and I has:

  if (culture == null && uiCulture == null)
  {
    // No values specified for either so no match
    return NullProviderCultureResult;
  }

Can't I define what to do when a NullProviderCultureResult occurs?

This may not answer your question, but at least will explain why it works like this.

But I am only able to access About page using:

/en/about
/pt/about

When I try to access "/about" the url is not changed but the homepage is rendered.

That is basically because the url "/about" is matching with the route /{culture} and so it assumes that "culture=about" , as a result it redirects to the index page because it matches the route:

Index: @page "{culture?}"

However, since there is no culture named "about" it renders the default culture "pt" in your case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM