在C#-Restsharp中序列化日期时间字段时,模型始终为空
我对C#中的简单序列化和反序列化有问题 我正在使用RestSharp调用webapi方法(REST方法) 型号为:在C#-Restsharp中序列化日期时间字段时,模型始终为空,c#,serialization,asp.net-web-api,asp.net-web-api2,restsharp,C#,Serialization,Asp.net Web Api,Asp.net Web Api2,Restsharp,我对C#中的简单序列化和反序列化有问题 我正在使用RestSharp调用webapi方法(REST方法) 型号为: public class MyModel { public DateTime date {get;set;} } [RoutePrefix("Test")] public class ValuesController : ApiController { [Route("~/Date")] [HttpPost] public IHttpActionRes
public class MyModel
{
public DateTime date {get;set;}
}
[RoutePrefix("Test")]
public class ValuesController : ApiController
{
[Route("~/Date")]
[HttpPost]
public IHttpActionResult Post([FromBody] MyModel model)
{
if (model == null)
return NotOk();
return Ok();
}
}
var restRequest = new RestRequest(@"http://localhost:50099/Date", Method.POST)
{
RequestFormat = DataFormat.Xml,
};
restRequest.AddBody(new MyModel(), "");
var restRequest = new RestRequest(@"http://localhost:50099/Date", Method.POST)
{
RequestFormat = DataFormat.Json,
};
restRequest.AddBody(new MyModel(), "");
控制器方法:
public class MyModel
{
public DateTime date {get;set;}
}
[RoutePrefix("Test")]
public class ValuesController : ApiController
{
[Route("~/Date")]
[HttpPost]
public IHttpActionResult Post([FromBody] MyModel model)
{
if (model == null)
return NotOk();
return Ok();
}
}
var restRequest = new RestRequest(@"http://localhost:50099/Date", Method.POST)
{
RequestFormat = DataFormat.Xml,
};
restRequest.AddBody(new MyModel(), "");
var restRequest = new RestRequest(@"http://localhost:50099/Date", Method.POST)
{
RequestFormat = DataFormat.Json,
};
restRequest.AddBody(new MyModel(), "");
但不幸的是,当使用xml
时,模型总是空的
Restsharp客户端:
public class MyModel
{
public DateTime date {get;set;}
}
[RoutePrefix("Test")]
public class ValuesController : ApiController
{
[Route("~/Date")]
[HttpPost]
public IHttpActionResult Post([FromBody] MyModel model)
{
if (model == null)
return NotOk();
return Ok();
}
}
var restRequest = new RestRequest(@"http://localhost:50099/Date", Method.POST)
{
RequestFormat = DataFormat.Xml,
};
restRequest.AddBody(new MyModel(), "");
var restRequest = new RestRequest(@"http://localhost:50099/Date", Method.POST)
{
RequestFormat = DataFormat.Json,
};
restRequest.AddBody(new MyModel(), "");
---->当数据格式为xml
时,模型为空
Restsharp客户端:
public class MyModel
{
public DateTime date {get;set;}
}
[RoutePrefix("Test")]
public class ValuesController : ApiController
{
[Route("~/Date")]
[HttpPost]
public IHttpActionResult Post([FromBody] MyModel model)
{
if (model == null)
return NotOk();
return Ok();
}
}
var restRequest = new RestRequest(@"http://localhost:50099/Date", Method.POST)
{
RequestFormat = DataFormat.Xml,
};
restRequest.AddBody(new MyModel(), "");
var restRequest = new RestRequest(@"http://localhost:50099/Date", Method.POST)
{
RequestFormat = DataFormat.Json,
};
restRequest.AddBody(new MyModel(), "");
----->当数据格式为json时,模型不为空。日期属性为默认值。已回答
简而言之,在初始化Json序列化程序时,我将DataContractSerializer
设置为true。此DataContractSerializer
预计日期为epoch
格式,因此出现问题。您是否对请求运行fiddler(网络)跟踪?-查看请求中的标题以及“内容类型”的设置将非常有趣。我检查了。content type
设置为text/xml
,您能告诉我们从跟踪中发布的xml是什么样子吗?/r/n 1/1/0001 12:00:00 AM/r/n
看看这篇文章()看起来像是一个类似的问题,通过完全指定xml名称空间可以解决。。。