2

I have a Web Api ASP.net CORE 1 MVC, c#. I make calls to the web api via a Client app I've built that is written using Angular (version 1).

I have a POST method that has a model with a date as a property.

In my development environment, when I post to the method, the date comes through fine, the model is valid and the date is in the format I expect "dd/MM/yyyy". However, when I move the code to my test server, the date comes through "MM/dd/yyyy". In actual fact, on the test server, the date comes to the code like this "0001-01-01T00:00:00".

I can see from recording the network traffic that the date being sent from the client app is correct. When it reaches the method it has been serialized by .net to the wrong format however, and the model state is invalid.

Has anyone had this problem before? Its so new that its hard to get good documentation on CORE 1 at the moment.

I've tried this code in my startup.cs for my web api:

 services.AddMvc().AddJsonOptions(opt =>
 {
 opt.SerializerSettings.DateFormatString = "dd/MM/yyyy";
 });

But it doesn't help things.

asked Aug 18, 2016 at 9:25
3

1 Answer 1

2

Usually to avoid confusion over serialisation of dates across localisations, it is advisable to serialise in an unambiguous format. In this case it would be better to format the date in the internationalised ISO format:

"YYYY-MM-DD",

See: http://www.iso.org/iso/home/standards/iso8601.htm

answered Aug 18, 2016 at 9:50
2
  • Ok - that's a fair point and I will definitely take note for future but I do have this dilemma now that I need to solve :-( Commented Aug 18, 2016 at 9:52
  • The only thing I can suggest is that you change the angular app to post in the ISO format. .NET core understands the ISO format and should deserialise correctly. Commented Aug 18, 2016 at 9:55

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.