Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# webapi在控制器中使用datetime反序列化JSON请求_C#_Json_Asp.net Web Api - Fatal编程技术网

C# webapi在控制器中使用datetime反序列化JSON请求

C# webapi在控制器中使用datetime反序列化JSON请求,c#,json,asp.net-web-api,C#,Json,Asp.net Web Api,备选案文1: 如果手动反序列化JSON请求,则会得到正确的结果: [Route("search")] [HttpPost] public IEnumerable<ErrorLogDto> Search(HttpRequestMessage request) { string res = request.Content.ReadAsStringAsync().Result; LogSearchDto dto = JsonConvert.DeserializeObject

备选案文1: 如果手动反序列化JSON请求,则会得到正确的结果:

[Route("search")]
[HttpPost]
public IEnumerable<ErrorLogDto> Search(HttpRequestMessage request)
{
    string res = request.Content.ReadAsStringAsync().Result;
    LogSearchDto dto = JsonConvert.DeserializeObject(res, typeof(LogSearchDto)) as LogSearchDto;
    //dto = not null
[路由(“搜索”)]
[HttpPost]
公共IEnumerable搜索(HttpRequestMessage请求)
{
string res=request.Content.ReadAsStringAsync().Result;
LogSearchDto dto=JsonConvert.DeserializeObject(res,typeof(LogSearchDto))作为LogSearchDto;
//dto=不为空
选项2:如果我使用此选项,我总是为dto获取空值:

[Route("search")]
[HttpPost]
public IEnumerable<ErrorLogDto> Search([FromBody]LogSearchDto dto)
{
// dto = null!
[路由(“搜索”)]
[HttpPost]
公共IEnumerable搜索([FromBody]LogSearchDto dto)
{
//dto=null!

我不知道如何调试它。

经过长时间的搜索,我通过从Global.asax.cs中删除以下行来解决问题:

var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.UseDataContractJsonSerializer = true;