0

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?

wohlstad
  • 12,661
  • 10
  • 26
  • 39
sjanisz
  • 57
  • 8
  • There *is* no content. 204 is the correct status code. Use `StatusCodeResult` if you want to return an explicit status only – Panagiotis Kanavos Dec 15 '22 at 11:14
  • I would agree but when I have set 201 it returned 201 when there was also no content, so what is the difference between 200 and 201 in this context? Looks inconsistent – sjanisz Dec 15 '22 at 11:21

1 Answers1

2

Why dont you just return a StatusCodeResult?!

return new StatusCodeResult(200);
silent
  • 14,494
  • 4
  • 46
  • 86
  • That is the solution to avoid using ObjectResult, I was trying to use answer from this question https://stackoverflow.com/a/37690852/8611327 but without success. Thanks. – sjanisz Dec 15 '22 at 11:27