ActionResult<T> should convert to OkObjectResult or set StatusCode to 200 #27165
Labels
affected-very-few
This issue impacts very few customers
area-mvc
Includes: MVC, Razor, Localization, CORS, most templates
enhancement
This issue represents an ask for new feature or an enhancement to an existing one
good first issue
Good for newcomers.
severity-minor
This label is used by an internal tool
Milestone
Currently if you return
Tin an action that returnsActionResult<T>it will create anObjectResult. The problem is that the StatusCode remains null rather than 200, even though the eventual response generated from it will be 200.This makes it confusing in, for example, an action filter, where a developer may only want to handle Ok object results on the executed context, including where
Tis returned directly. They may:OkObjectResultand find nullObjectResultwhich works but then find StatusCode is nullThey may not handle the result as they should as it's not obvious that a null StatusCode would eventually wind up as a 200 in the response. It seems to be an implementation detail that could easily change.
The assumption is always that returning
Twill result in a 200 OK (e.g. documentation mentions how you can leave out theTtype in aProducesResponseTypeattribute for 200 when usingActionResult<T>).Can this be further enforced by creating an
OkObjectResult(which will set StatusCode to 200 in its constructor)? If not, perhaps just anObjectResultas it currently does, but setting StatusCode to 200 with member initialisation?Is there perhaps room for improvement in documentation about any of this? Happy to be pointed to anything that makes it explicit how response status codes are created from result status codes including when the result status code is null etc.
The text was updated successfully, but these errors were encountered: