Date formats API + C#

Asked

Viewed 147 times

-2

I developed the system in layers, and I have 2 layers, where layer number 1 is my API and number 2 is where I keep my classes.

In one of the classes I have a field of type DateTime.

I created a method in my API that takes an object from this class.

I’m in charge of Postman, um request with the date attribute value of that object equal to 01/07/2018

But when the request hits the API the same attribute comes with the value 07/01/2018

As demonstrated below

POSTMAN
inserir a descrição da imagem aqui

API
inserir a descrição da imagem aqui

[HttpPost]
[Route("Salvar")]
public HttpResponseMessage Post(IntencaoDeCompraViewModel obj)
{
    ApiResult result = _intencaoDeCompraJsonAppServices.AddLead(obj);
    if (result.Status == MessageType.Error)
        return Request.CreateResponse(HttpStatusCode.BadRequest, result);

    return Request.CreateResponse(HttpStatusCode.OK, result);
}
  • Include your controller code and the section where you are assigning this input value and the conversion is being done. What is the version of mvc and . net framework?

  • I entered the controller code. The MVC version is 5 and the . NET version is 4.6.1

  • tried to put the <globalization culture="pt-BR" uiCulture="pt-BR" /> on web.config? and or declare the format in Viewmodel?

  • Sim, olha como está: <globalization enableClientBasedCulture="false" requestEncoding="utf-8" responseEncoding="utf-8" fileEncoding="iso-8859-1" responseHeaderEncoding="utf-8" resourceProviderFactoryType="string" enableBestFitResponseEncoding="true" culture="pt-BR" uiCulture="pt-BR" />

  • I couldn’t reproduce this behavior here... even changing the language the mvc didn’t change the date format

  • Is the problem in Postman?

  • I did it for Postman too, see if you don’t have any other settings in global.asax.Cs

  • Strange! I don’t know then rs

  • No global.asax ta normal tbm, I have nothing more rs. The test you did was layered or used the same layer class?

  • @Guilhermenunes You’ll need to configure the serializer to work with dates in the application culture. You use the standard Webapi 2 serializer?

  • I don’t know, to tell you the truth, I don’t know

Show 7 more comments

1 answer

-1

string dateDesejada = Datastartparentheme.Value.Tostring("dd-MM-yyyy"); Convert.Todatetime(date);

  • 1

    But then all endpoints that receive a date will need to have this code? If it is an object with 20 dates will have to repeat the code 20 times? And whenever someone adds a date field to the entry agreement they will need to remember to add this code?

Browser other questions tagged

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