Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.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# 对象未从JSON体传递到方法中_C#_Json_Asp.net Web Api2 - Fatal编程技术网

C# 对象未从JSON体传递到方法中

C# 对象未从JSON体传递到方法中,c#,json,asp.net-web-api2,C#,Json,Asp.net Web Api2,我正在尝试运行我在MVCAPI中创建的一个方法,该方法要求通过JSON格式的主体将一个对象传递给它。据我所知,该方法或对象没有错误,但我发送的JSON实际上没有传递到该方法中,并且对象参数仍然为null 方法如下: // Consignment Search [HttpPost] [ResponseType(typeof(Legacy.JobTracking))] [Route("api/search/")] public IHttpActionResult SearchConsignment

我正在尝试运行我在MVCAPI中创建的一个方法,该方法要求通过JSON格式的主体将一个对象传递给它。据我所知,该方法或对象没有错误,但我发送的JSON实际上没有传递到该方法中,并且对象参数仍然为null

方法如下:

 // Consignment Search
[HttpPost]
[ResponseType(typeof(Legacy.JobTracking))]
[Route("api/search/")]
public IHttpActionResult SearchConsignment(ConsignmentSearch search)
{ 
    // Get the ID
    var UserId = 0;
    using (_PortalDb)
    {
        var User = _PortalDb.PortalUsers.FirstOrDefault(u => u.ServerUser == search.User);
        if (User != null) UserId = User.UserId;
    }
    List<Legacy.JobTracking> Consignments;
    if (!ModelState.IsValid) return BadRequest("Invalid Search");
    if (search.JobNo == null && search.CustRef == null && search.ConRef == null && search.DateFrom == null && search.DateTo == null) return BadRequest("No search filters");
    if (search.JobNo != null)
    {
        // Run Job number search and continue
        Consignments = Legacy.Exporter.GetJobByNo(search.JobNo, search.User, search.Account, UserId);
    }
    else if (search.ConRef != null)
    {
        // Run Con Ref Search and continue
        Consignments = Legacy.Exporter.GetJobByCon(search.ConRef, search.User, search.Account, UserId);
    }
    else if (search.CustRef != null)
    {
        // Run Customer Ref Search and continue
        Consignments = Legacy.Exporter.GetJobByRef(search.CustRef, search.User, search.Account, UserId);
    }
    else if (search.DateFrom != null && search.DateTo != null)
    {
        // Run Date Range Search and continue
        Consignments = Legacy.Exporter.GetJobsInDateRange(search.DateTo, search.DateFrom, search.User, search.Account, UserId);
    }
    else
    {
        return BadRequest("Invalid Date Search");
    }
    return Ok(Consignments);
}
以及正文中的JSON:

["consignmentSearch": {
  "dateFrom": "20150101",
  "dateTo": 20160101,
  "user": "LianeS",
  "account": "PORTAL"
}]

您在请求主体中传递的文本不是有效的JSON(当然也不代表您的模型)。适用于您的案例的有效JSON如下所示:

{
  "dateFrom": "20150101",
  "dateTo": 20160101,
  "user": "LianeS",
  "account": "PORTAL"
}

太好了!谢谢
{
  "dateFrom": "20150101",
  "dateTo": 20160101,
  "user": "LianeS",
  "account": "PORTAL"
}