Home > Mobile >  new ObjectResult(null) StatusCode set to 200 returns 204
new ObjectResult(null) StatusCode set to 200 returns 204

Time:12-15

I have azure function returning Task<IActionResult>, when I create new ObjectResult object (Microsoft.AspNetCore.Mvc namespace), set its body to null and set StatusCode to 200 it actually returns 204 (no content).

ObjectResult objectResult = new ObjectResult(null);
objectResult.StatusCode = 200;
return objectResult;

But when I set objectResult.StatusCode to 201 (created) it returns created. I had to put dummy value to ObjectResult constructor parameter to finally get 200:

ObjectResult objectResult = new ObjectResult("autotest");

Is this behavior expected or is it bug?

CodePudding user response:

Why dont you just return a StatusCodeResult?!

return new StatusCodeResult(200);
  • Related