0

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?

Good Samaritan
  • 706
  • 1
  • 7
  • 21
  • 1.Please share a bit more of your code around your update method. 2. are you using any custom formatters / model binders ? – Gurpreet Sep 01 '17 at 12:07
  • I updated the question with the full Update() method, and as you can see it does not do much. About formatters, yes - I use a formatter for custom Json serializing. – Good Samaritan Sep 01 '17 at 12:28
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Igor Sep 01 '17 at 12:41
  • You need to explicitly check for a Null model object before attempting to do an update with it. That should avoid the null reference exception. – JohnH Sep 01 '17 at 13:27

2 Answers2

1

You don't have to return object:

[HttpPost]
public IActionResult Update()
{
    return Ok();
}
Timur Lemeshko
  • 2,747
  • 5
  • 27
  • 39
1
  1. Please try this code without any _profileManager.Update(model);
  2. Use IActionResult if you are using .net core.
  3. return Ok() does not force you to return anything and should work.
  4. if that does not work then check for any errors in your custom serializer.
  5. 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();
    }
    
Gurpreet
  • 1,382
  • 11
  • 24
  • Thanks, I'll try this and come back to you with the result. – Good Samaritan Sep 01 '17 at 12:42
  • Please try to unit test your serializer. that will lead you to the source of the problem quicker if there is any – Gurpreet Sep 01 '17 at 12:46
  • After commenting out all configuration statements in the Startup class, I found out that it was this line that created this misbehaviour: `WebApiConfig.MessageHandlers.Insert(0, new ServerCompressionHandler(new GZipCompressor(), new DeflateCompressor()));` This package even is deprecated, and I thus have to find another solution for handling request compression. [Package link](https://www.nuget.org/packages/Microsoft.AspNet.WebApi.MessageHandlers.Compression/) Thank you, @Gurpreet – Good Samaritan Sep 04 '17 at 11:19