Skip to content
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

[Breaking change]: ActionResult<T> set StatusCode to 200 #485

Open
1 of 2 tasks
brunolins16 opened this issue May 18, 2022 · 0 comments
Open
1 of 2 tasks

[Breaking change]: ActionResult<T> set StatusCode to 200 #485

brunolins16 opened this issue May 18, 2022 · 0 comments
Labels
6.0.0 Breaking change Documented The breaking change has been published to the .NET Core docs

Comments

@brunolins16
Copy link
Contributor

brunolins16 commented May 18, 2022

Description

When returning a T in a MVC/API Controller Action that declares the return type as ActionResult<T> will now always set the ObjectResult.StatusCode to 200, unless when the T is a ProblemDetails.

Since before this change the ObjectResult.StatusCode was null, in some scenarios where the status code is set manually, this change could cause unexpected behaviors. Also, an Action Filter could be affected by this change if it expects the null instead of 200.

Version

.NET 6

Previous behavior

Before if you have a Controller's Action that returns T and sets the Response.StatusCode manually, similar to the example:

// Generates a 202 Accepted response
public ActionResult<Model> Get()
{
    Response.StatusCode = StatusCodes.Status202Accepted;
    return new Model();
}

It will generate the expected 202 Accepted response status code.

New behavior

After the changes the same Controller's Action that returns T that sets the Response.StatusCode manually, will always generate a 200 OK response.

// Generates a 200 OK response
public ActionResult<Model> Get()
{
    Response.StatusCode = StatusCodes.Status202Accepted;
    return new Model();
}

Type of breaking change

  • Binary incompatible: Existing binaries may encounter a breaking change in behavior, such as failure to load/execute or different run-time behavior.
  • Source incompatible: Source code may encounter a breaking change in behavior when targeting the new runtime/component/SDK, such as compile errors or different run-time behavior.

Reason for change

This behavior is documented since ASP.NET Core 3.1 (https://docs.microsoft.com/en-us/aspnet/core/web-api/action-return-types?view=aspnetcore-3.1#actionresultt-type), however, it keeps the StatusCode as null that will eventually generate a 200 OK response as default. Since the default internal behavior could easily change, was decided to avoid relying on the internal default implementation and setting the StatusCode to the expected 200 OK.

Recommended action

If you are broken by this change, as the example mentioned before:

public ActionResult<Model> Get()
{
    Response.StatusCode = StatusCodes.Status202Accepted;
    return new Model();
}

You will need to change your Controller Action. These are some possible options that will keep the desired behavior:

public ActionResult<Model> Get()
{
   return Accepted(new Model());
}

//or

public ActionResult<Model> Get()
{
   return StatusCode(StatusCodes.Status202Accepted, new Model());
}

//or

public Model Get()
{
   Response.StatusCode = StatusCodes.Status202Accepted;
   return new Model();
}

Affected APIs

MVC/API Controller actions.

@aspnet aspnet locked and limited conversation to collaborators May 18, 2022
@gewarren gewarren added the Documented The breaking change has been published to the .NET Core docs label May 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
6.0.0 Breaking change Documented The breaking change has been published to the .NET Core docs
Projects
None yet
Development

No branches or pull requests

2 participants