dotnet / dotnet/aspnetcore

ControllerBase.Created returns NoContent and Ok results

Open
#53,734 2 comments 1 reaction 0 assignees View on GitHub
area-mvc
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

When I want to return 201 Created without any additional data, just like:
`return Created();`

it returns 204 NoContent.

When I change the NoContentFormatter behaviour:
```
builder.Services.AddControllers(o =>
{
var noContentFormatter = o.OutputFormatters.OfType().FirstOrDefault();
if(noContentFormatter != null)
{
noContentFormatter.TreatNullValueAsNoContent = false;
}
});
```

then `return Created();` return 200 OK.

So, if I want to return 201 Created I have to give some object in te response. This is very misleading.

### Expected Behavior

`return Created();` should return `201 Created` response.

If, for some reason, you MUST return something else than 201 when the body is empty, then please just remove the empty method overload and just left the overloads with needed parameters.

### Steps To Reproduce

_No response_

### Exceptions (if any)

_No response_

### .NET Version

8.0.1

### Anything else?

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.