When I post a json object with a date property to a ApiController it won't deserialize into a date.
Server site code:
public class MegaTestController : ApiController
{
// POST /megatest
public void Post(ttt value)
{
string sdf = "!sad";
}
}
public class ttt
{
public DateTime Date { get; set; }
public string Name { get; set; }
}
Then I do a POST request with fiddler
POST http://localhost:62990/MegaTest HTTP/1.1
User-Agent: Fiddler
Host: localhost:62990
Content-Type: text/json
Content-Length: 54
{ "Date":"/Date(1239018869048)/", "Name":"Dude" }
But the object coming in only has the Name
property set, the Date
property is {01.01.0001 00:00:00}
Am I missing any headers or project settings?
Edit: The requests are actually coming from a HttpClient
. Is it possible to format the date before sending the request over with HttpClient
?
public Task<T> Create<T>(T item)
{
var service = new HttpClient();
service.BaseAddress = new Uri("http://localhost:62990");
var method = typeof(T).Name + "s"; // in this case it will be ttts
var req = new HttpRequestMessage<T>(item);
req.Content.Headers.ContentType = new MediaTypeHeaderValue("text/json");
return service.PostAsync(method, req.Content).ContinueWith((reslutTask) =>
{
return reslutTask.Result.Content.ReadAsAsync<T>();
}).Unwrap();
}
var data = new ttt { Name = "Dude", Date = DateTime.Now };
Create(data);
Edit: This is a known bug with the ASP MVC 4 Beta and the final version of ASP MVC 4 will use Json.net as a json serializer until then you can use the default XML serializer or switch out the default Json serializer for Json.net. More info can be found on hanselman blog
It appears that Web API doesn't accept dates in the old ASP.NET AJAX format for URL encoded POST data. There seems to be two formats that it accepts URL encoded dates in currently:
ShortDateString: "2/23/2012"
ISO: "2012-02-23T00:00:00"
The later is the ISO DateTime format, and there are a variety of code snippets you can find to help with converting a JavaScript Date object to that format. Several mentioned here: How do I output an ISO 8601 formatted string in JavaScript?
Web API does still accept the /Date()/ format if you send that data as JSON and set the Content-Type correctly though:
$.ajax({
url: 'MegaTest',
type: 'POST',
// Setting this Content-Type and sending the data as a JSON string
// is what makes the old /Date()/ format work.
contentType: 'application/json',
data: '{ "Date":"/Date(1239018869048)/", "Name":"Dude" }'
});
Try to post your date/time as "yyyy-MM-dd HH:mm:ss". ASP MVC will handle it properly.
This is a known bug with the ASP MVC 4 Beta and the final version of ASP MVC 4 will use Json.net as a json serializer until then you can use the default XML serializer or switch out the default Json serializer for Json.net. More info can be found on hanselman blog
Here is a small example of using the default XML Serializer to send DateTime
with HttpClient:
var service = new HttpClient();
service.BaseAddress = url;
var mediaType = new MediaTypeHeaderValue("application/xml");
XmlMediaTypeFormatter formater = new XmlMediaTypeFormatter();
var req = new HttpRequestMessage<T>(item, mediaType, new MediaTypeFormatter[] { formater });
service.PutAsync(method, req.Content);
But if you would like to use json then here is a good blog post on using JSON.NET with ASP.NET Web API
ASP.Net Web API uses the DataContractJsonSerializer
which has bugs surrounding serialization of DateTime
. You should use JSON.Net instead, and implement a MediaTypeFormatter
that uses JSON.Net instead of DataContractJsonSerializer. Check out my answer for a similar question here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With