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.
-
Please read stackoverflow.com/help/tagging FAQ to know how to correctly use tags and avoid forcing tags into the question title, no "thanks" at the end and please read the description of the asp.net-mvc6 before using it! stackoverflow.com/tags/asp.net-mvc6/infoTseng– Tseng08/18/2016 09:39:28Commented Aug 18, 2016 at 9:39
-
That tag shouldnt even be publicly accessible currently if it's for a 'potential' future version of the legacy webstack.BenM– BenM08/18/2016 10:51:39Commented Aug 18, 2016 at 10:51
-
1@BenM: There are already burnitate requests for it meta.stackoverflow.com/questions/317858/burninate-mvc6 and meta.stackoverflow.com/questions/322140/… and meta.stackoverflow.com/questions/315270/… but seems no one really cares and making asp.net-mvc6 a synonym for asp.net-core-mvc is just worng as there is still a chance that a real MVC6 comes which is based on the legacy frameworkTseng– Tseng08/18/2016 11:27:07Commented Aug 18, 2016 at 11:27
1 Answer 1
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",
-
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 :-(Rob– Rob08/18/2016 09:52:30Commented 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.Slicc– Slicc08/18/2016 09:55:20Commented Aug 18, 2016 at 9:55
Explore related questions
See similar questions with these tags.