-
Notifications
You must be signed in to change notification settings - Fork 10k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ControllerBase.Ok(object value) responds with 204 status code when value is null #8847
Comments
This behaviour is done by services.AddMvc(options =>
{
var noContentFormatter = options.OutputFormatters.OfType<HttpNoContentOutputFormatter>().FirstOrDefault();
if (noContentFormatter != null)
{
noContentFormatter.TreatNullValueAsNoContent = false;
}
}); The behaviour is indeed confusing. |
This is especially confusing because an action method that returns |
I do not think this is a bug, it works as intended (although confusing). It has been like this for a couple of years now. Any change to this behaviour would be a big breaking change at this point ;) |
I'm not sure how many people are relying on the behavior of Is there a reason that the I believe that this behavior should at least be disabled when using Setting the status code to 204 when using I understand what If this is a big breaking change, then right now is the perfect time to do it since .net core is moving to its next major version. |
The XML comment for
Worse, if you inspect the code for OkObjectResult, it appears like it should be using a 200 status code no matter what. That this behavior happens in the pipeline makes it difficult to diagnose. It also makes testing GET requests in the browser via the address bar difficult, as when 204 is returned, it doesn't change pages. Returning 204 No Content when the code explicitly is requesting a 200 OK response is confusing, unexpected, inconsistent with the documentation, and inconsistent with common wisdom that 204 is primarily for PUT/DELETE requests when in this case 204 would be returned even if the method is GET. I'm in favor of making this a breaking change to revert it to the expected behavior and not add the |
Ran into this myself today. Passed a null to I wound up just putting a method in my base controller to deal with it:
|
We should fix this. It's bad if we're not honouring user expectations in this case. |
This comment has been minimized.
This comment has been minimized.
@rynowak Can I take a stab at this? |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@syndicatedshannon \ @cremor this sounds different to the issue at hand. Please file a separate issue. @AlexejTimonin could you outline your proposal here before you send a PR? Thanks. |
Sure. Thanks for the feedback. |
Done. Now, back to my original comment, I'm not sure the entire issue as reported here is accurate:
After doing more research to open issue #16925 , I'm pretty confident that |
@syndicatedshannon In an API method that returns That said, for I am not aware of any other status code methods that return status codes other than what they are named. i.e. does |
@paulirwin Totally makes sense. Conventionally, what should the payload be on a JSON-serialized 200 Ok()? |
@syndicatedshannon Based on the principle of least surprise, my expectation would be that it would be an empty body if you call Edit: in fact, that's what the XML documentation says will happen for the
|
Also, I misunderstood:
I thought |
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process. |
I agree that Ok(null) that returns 204 is somewhat unintuitive, but I wonder how this could be fixed, since the output formatter seems to be at a higher level. I'd like to have this behavior removed by default, but that's probably not easily doable now (breaking change) |
As @jeremyVignelles stated the currently the We can change the api to provide additional information to the context, however, the current workaround seems a reasonable approach #8847 (comment), and I don't know if changing the default behavior will help in general (besides the fact that I agree that the API is confusing). @pranavkm do you have any different thoughts? |
This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes. See our Issue Management Policies for more information. |
So now it has been clear that it won't be fixed, I guess the only thing that can be done is to fix the ApiExplorer to support that behavior : #28060 |
Describe the bug
Returning Ok(null) from a controller action results in a 204 status code, rather than a 200. I'm not sure if this in intended, but it's not what I would expect.
To Reproduce
Steps to reproduce the behavior:
Expected behavior
Response status code should always be 200 when returning
Ok()
orOk(object value)
, regardless of if value is null.The text was updated successfully, but these errors were encountered: