I am having an issue with simple serialization and deserialization in C#.
I am using RestSharp to call a webapi method (REST method).
Model is:
public class MyModel
{
public DateTime date {get;set;}
}
Controller Method:
[RoutePrefix("Test")]
public class ValuesController : ApiController
{
[Route("~/Date")]
[HttpPost]
public IHttpActionResult Post([FromBody] MyModel model)
{
if (model == null)
return NotOk();
return Ok();
}
}
But unfortunately, the model
is always null when using xml
.
Restsharp Client:
var restRequest = new RestRequest(@"http://localhost:50099/Date", Method.POST)
{
RequestFormat = DataFormat.Xml,
};
restRequest.AddBody(new MyModel(), "");
----> when data format is xml
, the model is null.
Restsharp Client:
var restRequest = new RestRequest(@"http://localhost:50099/Date", Method.POST)
{
RequestFormat = DataFormat.Json,
};
restRequest.AddBody(new MyModel(), "");
-----> when data format is json, the model is not null. The date property is default.
Answered here .
In short, I set DataContractSerializer
to be true while initializing Json serializer
. This DataContractSerializer
expected date in epoch
format and hence the issue.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.