2

My Web API is as under

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

  return leadRepository.SearchLead(searchCriteria);
}

I am invoking as

http://localhost:52388/LeadAPI/SearchLead

But I am getting NULL Reference exception.

public class LeadSearchCriteria
    {
        LeadSearchCriteria() { }
        public int ProductID { get; set; }
        public int ProductProgramId { get; set; }        
    }

What mistake I am making?

Postman

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

ERROR

{
    "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()"
}
3
  • you are not passing the LeadSearchCriteria object that's why u r getting null reference exception Commented May 28, 2015 at 18:37
  • Your api get request is expecting 'LeadSearchCriteria'. try to pass object and then let me know Commented May 28, 2015 at 18:39
  • No not working.... i have updated my question Commented May 28, 2015 at 19:03

1 Answer 1

2

Your controller method needs you to pass JSON or XML representation of your object in request so that it can use it. You can also pass it through Uri with [FromUri] annotation before parameter type.

Try POSTing JSON like this in your request:

{
 "ProductID": 1,
 "ProductProgramID": 2
}

Alternatively you can edit your Get method like this:

[HttpGet]
[Route("SearchLead")]
public IEnumerable<LeadSearchResult> SearchLead1(int productID, int productProgramID)
{
  //edit SearchLead to make use of integer params
  return leadRepository.SearchLead(productID, productProgramID);
}

And send request like this:

http://myapi:12345/SearchLead?productID=1&productProgramID=2

Edit:

As I already said, you are trying to POST object to controller, so change your method to 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);
}

Please note that you should use your business logic inside method above, I just shown out of the box example for fast testing purposes.

Sign up to request clarification or add additional context in comments.

5 Comments

But I can even pass null values too...in that case how to pass the data?
the second option is working in the current.... i just want to do it in the first way
I dont really understand what you're asking. You can pass null because you allow it. Validate model you're sending in controller's method so you won't allow it anymore. If you have trouble sending objects, try tools like Postman on https://www.getpostman.com/ that allow you to create REST calls with easy to use GUI.
One more thing: if I'm not mistaken, you need to actually POST the data to controller in order to send object in. I could be wrong as I generally go parameters way.
Updated for example of POST to GET (not a best approach for REST in general though...).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.