Api gets date with changed month and day if parameter is sent by url

Asked

Viewed 103 times

0

I have an api that gets a date (the date is sent as string and received as Datetime):

    [HttpPost("ObterDados")]
    public JsonResult ObterDados(DateTime data)
    {
       //codigo

if I post with the parameter in the url the date is received with the month and day reversed (it receives mm/dd/yyyy)

    $.post("/api/ObterDados/?data=" + self.Data...

if I send the date inside an object it is received correctly (gets dd/mm/yyyy)

    var objeto = {data: "15/12/2017"}

    $.post("/api/ObterDados/", objeto....

    [HttpPost("ObterDados")]
    public JsonResult ObterDados(Filtro filtro)
    {
       //codigo

Why does this happen and has some way to always receive the date as dd/mm/yyyy ?

Note: The date is always sent as string

Configuration of the Statup class:

        var requestLocalizationOptions = new RequestLocalizationOptions
        {

            SupportedCultures = new List<CultureInfo>
            {
                new CultureInfo("pt-BR")
            },
            SupportedUICultures = new List<CultureInfo>
            {
                new CultureInfo("pt-BR")
            },

            DefaultRequestCulture = new RequestCulture("pt-BR", "pt-BR"),
        };

        app.UseRequestLocalization(requestLocalizationOptions);

Web.config:

<?xml version="1.0" encoding="utf-8"?>
  <configuration>
    <system.webServer>
      <handlers>
        <add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified"/>
      </handlers>
      <httpPlatform processPath="%DNX_PATH%" arguments="%DNX_ARGS%" stdoutLogEnabled="false" startupTimeLimit="3600"/>
    </system.webServer>
  </configuration>
  • Get a look at this question https://stackoverflow.com/questions/24690879/how-use-a-specific-cultureinfo-in-asp-net-web-api

  • Just set up Cultureinfo on your Webconfig

  • With this configuration Binder will do Datetime.Parse in the default culture of the application, that is, whatever is configured in the config. If you want you can do it via code, but I find it more efficient to do it in Config.

  • I put the configuration of the Startup class (Asp.net core 2.0) in the question, give a look, it would be the same thing to do that you suggested in webconfig ?

  • Gee, it’s different in . net core?

  • I think so, since I created the project I haven’t changed the web.config, nor the connection to the bank..

  • You should have <Globalization> on the web.config I see no reason not to have :-\

  • Take a look at the accepted answer there, it puts on the Web.Config, because by Web.Config you talk to the IIS I see no reason for the . net core have modified this.

  • I will try the solution of the answer accepted, I put the project web.config in question..

  • Take a look at the Core version of Globalization https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization

  • 1

    Send as YYYY-MM-DD, which is an unambiguous format.

Show 6 more comments

1 answer

0


After following Several tutorials, having tried several ways to fix this by configuring the application, the best option was to create a component using Vue.js, for inputs that are date. It formats the date in dd/mm/yyyy on the screen but in the value the date is in the format yyyy/mm/dd. So I don’t need to use functions every time I send the date to the api.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.