C# Web API参数调用中出现空引用异常

C# Web API参数调用中出现空引用异常,c#,asp.net-web-api,C#,Asp.net Web Api,我的Web API如下所示 [HttpGet] [Route("SearchLead")] public IEnumerable<LeadSearchResult> SearchLead1(LeadSearchCriteria searchCriteria) { return leadRepository.SearchLead(searchCriteria); } 我犯了什么错误 邮递员 { "ProductID": 1, "ProductProgramId": 1

我的Web API如下所示

[HttpGet]
[Route("SearchLead")]
public IEnumerable<LeadSearchResult> SearchLead1(LeadSearchCriteria searchCriteria)
{

  return leadRepository.SearchLead(searchCriteria);
}
我犯了什么错误

邮递员

{
  "ProductID": 1,
  "ProductProgramId": 1
}
错误

{
    "Message": "An error has occurred.",
    "ExceptionMessage": "Object reference not set to an instance of an object.",
    "ExceptionType": "System.NullReferenceException",
    "StackTrace": "   at System.Web.Http.ApiController.<InvokeActionWithExceptionFilters>d__1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__0.MoveNext()"
}
{
“消息”:“发生错误。”,
“ExceptionMessage”:“对象引用未设置为对象的实例。”,
“ExceptionType”:“System.NullReferenceException”,
“StackTrace”:“在System.Web.Http.ApiController.d_u1.MoveNext()\r\n--从引发异常的上一个位置开始的堆栈结束跟踪----\r\n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务任务)\r\n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务任务任务任务任务任务)\r\n位于System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n位于System.Web.Http.Dispatcher.HttpControllerDispatcher.d\u 0.MoveNext()
}

您的控制器方法需要您在请求中传递对象的JSON或XML表示,以便它可以使用它。您还可以在参数类型之前通过带有[FromUri]注释的Uri传递它

尝试在您的请求中发布JSON,如下所示:

{
 "ProductID": 1,
 "ProductProgramID": 2
}
或者,您可以像下面这样编辑Get方法:

[HttpGet]
[Route("SearchLead")]
public IEnumerable<LeadSearchResult> SearchLead1(int productID, int productProgramID)
{
  //edit SearchLead to make use of integer params
  return leadRepository.SearchLead(productID, productProgramID);
}
http://myapi:12345/SearchLead?productID=1&productProgramID=2
编辑:

正如我已经说过的,您正在尝试将对象发布到控制器,所以请将您的方法更改为HttpPost:

[HttpPost]
[Route("SearchLead")]
public IEnumerable<LeadSearchResult> PostCriteria([FromBody]LeadSearchCriteria criteria)
{
    List<LeadSearchResult> list = new List<LeadSearchResult>() { new LeadSearchResult { ProductID = 1, Result = 12 }, new LeadSearchResult { ProductID = 2, Result = 22 } };
    return list.Where(x => x.ProductID == criteria.ProductID);
}
[HttpPost]
[路线(“搜索线索”)]
公共IEnumerable后标准([FromBody]LeadSearchCriteria标准)
{
List List=new List(){new LeadSearchResult{ProductID=1,Result=12},new LeadSearchResult{ProductID=2,Result=22};
返回列表。其中(x=>x.ProductID==criteria.ProductID);
}

请注意,您应该在上面的方法中使用您的业务逻辑,为了快速测试,我刚刚展示了开箱即用的示例。

您没有传递LeadSearchCriteria对象,这就是为什么您获取空引用例外api获取请求需要“LeadSearchCriteria”。试着通过物体,然后让我知道不工作。。。。我已经更新了我的问题,但我甚至可以传递空值…在这种情况下,如何传递数据?第二个选项在当前。。。。我只是想用第一种方式来做,我真的不明白你在问什么。您可以传递null,因为您允许它。验证您在控制器方法中发送的模型,以便不再允许它。如果发送对象时遇到问题,请尝试
on上的
Postman
https://www.getpostman.com/
允许您使用易于使用的GUI创建REST调用。还有一件事:如果我没有弄错的话,您需要将数据发送到控制器,以便将对象发送进来。我可能是错的,因为我通常会这样做。例如更新了POST to GET(虽然不是REST的最佳方法…)。
[HttpPost]
[Route("SearchLead")]
public IEnumerable<LeadSearchResult> PostCriteria([FromBody]LeadSearchCriteria criteria)
{
    List<LeadSearchResult> list = new List<LeadSearchResult>() { new LeadSearchResult { ProductID = 1, Result = 12 }, new LeadSearchResult { ProductID = 2, Result = 22 } };
    return list.Where(x => x.ProductID == criteria.ProductID);
}