How to bypass ASP.NET Core culture-sensitive conversion of form data?

58 views Asked by At

I am using ASP.NET Core Localization middleware in my application to pick up the AspNetCore.Culture cookie value.

services.Configure<RequestLocalizationOptions>(options =>
{
    options.DefaultRequestCulture = new RequestCulture(myDefaultCulture, myDefaultCulture);
    options.SupportedCultures = options.SupportedUICultures = mySupportedCultures;

    options.RequestCultureProviders.Clear();
    options.RequestCultureProviders.Add(new CookieRequestCultureProvider());
});

Now, I have an action which picks up browser geo-location values submitted from my client form:

<div>
    <input id="lat" name="lat" value="">
    <input id="long" name="long" value="">
</div>

Looking at my http request, the values are something like 27.67807948515076 and -109.11917758267799. Since my action is taking two double values, it works fine when the request's cookie is en-us. But if the AspNetCore.Culture cookie is set to es-es, my action is getting value like 2767807948515076 and -10911917758267799. Seems like it is because ASP.NET Core will let form data to go through culture-sensitive conversion by design.

What is the best practice to handle this case? Should my client detect the cookie setting and send data in different format? Or should I try to bypass the culture-sensitive conversion on server side? I guess I should not since it is by design for a reason. But if I need to, how to achieve that?

I tried to use string types in my action signature and try to parse the value to double. It works. But I feel this is a common case that should have a better solution that manually handle this in code logic.

0

There are 0 answers