question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[FromBody]/ JSON binding doesn't use Culture from RequestCultureProviders

See original GitHub issue

Describe the bug

Culture derived from RequestCultureProviders is not being implemented in JSON Input binding. The culture received is being used in query string, application/x-www-form-urlencoded binding but not working with application/json.

To Reproduce

https://github.com/ruchanb/AspNetCoreCultureModelBind This is standard new project build with addition of

var supportedCultures = new System.Collections.Generic.List<CultureInfo>
            {
                new CultureInfo("en-US"),
                new CultureInfo("es-MX")
            };

            var requestLocalizationOptions = new RequestLocalizationOptions
            {
                SupportedCultures = supportedCultures,
                SupportedUICultures = supportedCultures,
            };
            //requestLocalizationOptions.DefaultRequestCulture = new RequestCulture(culture: "en-US", uiCulture: "en-US");
            requestLocalizationOptions.RequestCultureProviders.Insert(0, new JsonRequestCultureProvider());
app.UseRequestLocalization(requestLocalizationOptions);

provider

public class JsonRequestCultureProvider : RequestCultureProvider
    {
        public override Task<ProviderCultureResult> DetermineProviderCultureResult(HttpContext httpContext)
        {
            if (httpContext == null)
            {
                throw new ArgumentNullException(nameof(httpContext));
            }
            var header = httpContext.Request.Headers["culture"];
            var culture = header.Count == 0 ? "en-US" : header[0]; // Use the value defined in config files or the default value
            var uiCulture = culture;
            return Task.FromResult(new ProviderCultureResult(culture, uiCulture));
        }
}

Controller

 [Route("api/[controller]/[action]")]
    public class ValuesController : ControllerBase
    {
        [HttpGet]
        public IActionResult Date1(DateTime date)
        {
            return Ok(date);
        }
        [HttpPost]
        public IActionResult Date2(DateModel model)
        {
            return Ok(model);
        }
        [HttpPost]
        public IActionResult Date3([FromBody]DateModel model)
        {
            return Ok(model);
        }
}
public class DateModel
    {
        public DateTime Date { get; set; }
}

Here are some cURL commands to run when we start the application

curl http://localhost:20991/api/values/date1?date=5/15/2019
curl http://localhost:20991/api/values/date1?date=15/5/2019 -H "culture: es-MX"
curl http://localhost:20991/api/values/date2 -X POST -d "Date=5/15/2019"
curl http://localhost:20991/api/values/date2 -X POST -d "Date=15/5/2019" -H "culture: es-MX"
curl http://localhost:20991/api/values/date3 -X POST -d "{\"Date\":\"5\/15\/2019\"}" -H "Content-Type: application/json"

curl http://localhost:20991/api/values/date3 -X POST -d "{\"Date\":\"15\/5\/2019\"}" -H "culture: es-MX" -H "Content-Type: application/json"

Expected behavior

The expected behavior is to return date in ISO format, with 5 as Month, 15 as Day and 2019 as Year.

All the commands pass except the last one with application/json content-type and es-MX as culture. Here it takes the input as invalid.

Additional context

I am running the project on asp.net core 2.2 visual studio 2017. Issue is replicated also on 2.1 and 1.1 versions as I have checked.

I am aware this could, not be a bug, if so please provide information on how can I implement this behavior. Related SO question: https://stackoverflow.com/questions/55951070/datetime-input-to-deserialize-from-users-culture-instead-of-servers-asp-net-c

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
JamesNKcommented, May 16, 2019

I think MvcNewtonsoftJsonOptions can get public bool ReadJsonWithRequestCulture { get; set; }. When true CultureInfo.CurrentCulture is set onto the reader.

Thoughts?

0reactions
JamesNKcommented, May 17, 2019

An ISO date is a good idea because it does not use culture.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[FromBody]/ JSON binding doesn't use Culture from ...
Culture derived from RequestCultureProviders is not being implemented in JSON Input binding. The culture received is being used in query string ...
Read more >
c# - WebAPI POST [FromBody] not binding
I'm posting JSON to a WebAPI controller, but the properties on the model are not being bound. ... Every example I can find...
Read more >
Parameter Binding in ASP.NET Web API - ASP.NET 4.x
When a parameter has [FromBody], Web API uses the Content-Type header to select a formatter. In this example, the content type is "application/ ......
Read more >
Parameter binding in Minimal API applications
Parameter binding is the process of converting request data into strongly typed parameters that are expressed by route handlers.
Read more >
NET 6 API unable to bind JSON to model on POST requests
I have another API which I built last week which accepts a POST request, using all the same config and structure as this...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found