Question: ASP.NET Web API Globalization Input Format

Question

ASP.NET Web API Globalization Input Format

Answers 1
Added at 2017-01-04 15:01
Tags
Question

ASP Web API and ASP Controllers have auto conversion for inputs:

[HttpGet]
public IActionResult Get(decimal? money, DateTime dateTime){
    // some logic
}

[HttpPost]
public IActionResult Post(decimal? money, DateTime dateTime){
    // some logic
}

HTTP 1.1 is passed as a text format, not a binary, so it is important to follow exact string format. There is a good article about .NET Core Globalization and Localization setup, however it is not clear what Culture is used for auto parsing parameters.

  1. What should assume a client about NumberInfo and DateTime format?

    Post("1.2", "31.01.2017") or Post("1,2", "01/31/2017")
    

    If we set up on the server UseClientCulture and UseClientUICulture, will it affect the input parsing? Or is Microsoft always using InvariantCulture for parsing at this step?

  2. What about a JavaScript number? Is JavaScript number object passed in the invariant culture format or is it converted to a client/browser culture string represantation during HTTP transfer?

Answers
nr: #1 dodano: 2017-01-04 16:01

You must follow guidelines described in the Localization is configured in the ConfigureServices method section of the link you provided, where it clearly says:

Localization is configured in the ConfigureServices method

Since Auto conversion will perform conversions based on Current Thread CultureInfo instance, you must pick one of the proposed strategy to be able to set the appropriate CultureInfo and let the autoconversion magic happen.

Edit

  1. What should assume a client about NumberInfo and DateTime format?

It's the client the one telling the server which culture it's willing to pass, by specifing Accept-Language header, or by using a culture param embedded within an url.

A web service is not supposed to support every language a client would like to use, but it's web service responsibility to decline such unsupported requests.

  1. What about a JavaScript number? Is JavaScript number object passed in the invariant culture format or is it converted to a client/browser culture string represantation during HTTP transfer?

You must esplicitly pass a culture-aware value, as explained in another answer on SO.

Source Show
◀ Wstecz