I have an ASP.Net Core 3.1 app with the following startup.cs (I have tried various combinations of the below configuration based on web searches):
public void ConfigureServices(IServiceCollection services)
{
...
services.Configure<RequestLocalizationOptions>(options =>
{
options.RequestCultureProviders = new[] { new CookieRequestCultureProvider() };
});
and
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
...
var options = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(options.Value);
And in my app's logon method, I'm setting the Culture Cookie as follows:
HttpContext.Response.Cookies.Append(
CookieRequestCultureProvider.DefaultCookieName,
CookieRequestCultureProvider.MakeCookieValue(new RequestCulture([logged-in-user].CultureCode)));
In subsequent requests I can see the cookie in my browser dev tools Network tab:
Yet, on the server, any given request still maintains the default server culture (which is en-ZA
) for my dev environment. (I'm seeing this by checking System.Threading.Thread.CurrentThread.CurrentCulture.Name
in any breakpoint in my server action methods)
And I'm running into date conversion issues between my client and my server (e,g client with en-US
culture as per screenshot above) sends a date of 3/5/2009 (March 5th) to the server, and the server is interpreting it as May 3rd.
Why is my server not honoring the CultureCookie? What am I missing?
The default providers are : If the user has a cookie named “.AspNetCore.Culture” and the contents is in the format of “c=en-nz|uic=en-nz”, then it will override the culture. Your browser by default actually sends through a culture that it wishes to use.
Culture in ASP.net has always been a bit finicky to get right. In ASP.NET Core there is no exception but with the addition of middleware, things are now configured a little differently. Lifting and shifting your standard ASP.NET code may not always work in ASP.NET Core, and at the very least, it won’t be done in the new “ASP.NET Core way”.
Lifting and shifting your standard ASP.NET code may not always work in ASP.NET Core, and at the very least, it won’t be done in the new “ASP.NET Core way”. It should be noted that there are two “cultures” in the .NET eco system. The first is labelled just “Culture”.
According to the AspNet caching docs, when a cookie is set, the response shouldn't be cached, but if that particular URL was previously cached by the proxy, wouldn't the AspNet Cache-Control headers be be moot?
As you mentioned, you have registered your localization service in your ConfigureServices
method.
My suggested way is to use it like:
services.AddLocalization(options => options.ResourcesPath = "Resources");
services
.AddControllersWithViews()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
But don't forget to register the middleware:
// In StartUp.cs Configure method
var SupportedCultures = new CultureInfo[]
{
new CultureInfo("en"),
new CultureInfo("zh")
};
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture(defaultLanguage),
SupportedCultures = SupportedCultures,
SupportedUICultures = SupportedCultures
});
As for your cookie end-time issue, please try to specify the end date of your cookie. Like this:
Response.Cookies.Append(
CookieRequestCultureProvider.DefaultCookieName,
CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
new CookieOptions
{
Expires = DateTimeOffset.UtcNow.AddYears(1),
SameSite = SameSiteMode.None
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With