ActionResult OK returns 204 on an empty payload instead of 200
mika76 opened this issue · comments
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
This bug has been reported before in #8847 but was unceremoniously closed as working by design, all the while everyone in the thread (including @brunolins16 who closed the bug) has said that it is confusing and bad. I would more state that it is still a bug and should be fixed. (also why is the issue closed to discussion? Nobody was getting rowdy or rude...)
The fact is that when I say I want to return ActionResult
of Ok()
that is a specific status code and it is what should be returned. There is a specific method to return NoContent
. So basically the API is not honoring what the developer has requested.
workaround...
services.AddMvc(options =>
{
var noContentFormatter = options.OutputFormatters.OfType<HttpNoContentOutputFormatter>().FirstOrDefault();
if (noContentFormatter != null)
{
noContentFormatter.TreatNullValueAsNoContent = false;
}
});
Yes there is a way to switch it off - but it's a global change and it means every single method needs to be re-tested to make sure the right status code is being returned - and it's the most important and used status code of all.
So my proposal is, change the default setting of HttpNoContentOutputFormatter.TreatNullValueAsNoContent
to false
and make it opt-in instead of opt-out. This was a bad default and should be fixed.
Documentation
I must also add that the xml documentation of Ok()
says "Gets the HTTP status code: Status200OK" so the documentation is wrong too.
Expected Behavior
Expected behavior is for the status code asked for to be returned. If I want Ok() it should return 200, if NoContent() then 204.
Steps To Reproduce
Return Ok(null)
from a controller method which returns 'ActionResult`
Exceptions (if any)
No response
.NET Version
8
Anything else?
No response