Reputation: 706
I have a restful WebAPI application that handles requests from an AngularJS app. The controller actions requiring the GET verb works fine, but actions with POST/PUT that does not return any content will throw a Nullreference Exception.
Example:
[HttpPut]
public void Update()
This gives me the following message after function execution returns:
System.NullReferenceException: Object reference not set to an instance of an object. <SendAsync>d__0.MoveNext
It will also return HTTP status code 204 to the client.
By changing method signature to be async and return an empty object it will work as expected:
[HttpPut]
[Route("")]
public async Task<object> Update(ProfileViewModel model)
{
_profileManager.Update(model);
return Ok(new {});
}
Note that just returning status code 200 will not work. Some content must also be returned, or else the exception is thrown.
This happens on every request that is not GET, including DELETE. How can I fix this without having to change the signature and return an anonymous object for every single method?
Upvotes: 0
Views: 96
Reputation: 1442
if that WORKS then put back your _profileManager.Update(model) and check if that is throwing any errors.
[HttpPut]
[Route("")]
public async Task<IHttpActionResult> Update(ProfileViewModel model)
{
return Ok();
}
Upvotes: 1
Reputation: 2879
You don't have to return object:
[HttpPost]
public IActionResult Update()
{
return Ok();
}
Upvotes: 1