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.
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:However, since there is no culture named "about" it renders the default culture "pt" in your case.