Description
Describe the bug
The DefaultApiConventions should define a Status204NoContent
ResponseType in addition to the currently defined ResponseTypes.
Right now many documentation pages and tutorials return NoContent
in their Delete
method. When you apply the DefaultApiConventions
and add web API analyzers to those APIs, the analyzers will show warnings about the undeclared status code 204.
To Reproduce
Steps to reproduce the behavior:
- Create a Web API that returns
NoContent
for aDelete
method, e.g. by following this tutorial: Create a web API - Apply the
[ApiConventionType(typeof(DefaultApiConventions))]
attribute to the controller or assembly. - Add a reference to
Microsoft.AspNetCore.Mvc.Api.Analyzers
to the project. - Check the warnings.
Expected behavior
A Delete
method should be able to return NoContent()
with default conventions applied without getting a warning from API analyzers.
Additional context
This doesn't affect Delete
methods that return void
(or Task
) because for some reason ASP.NET Core returns HTTP 200 (with no body) for those (btw, why is this? For Web API 2 it was even documented that void
returns 204). But as soon as you want to handle cases like returning NotFound
, you'd need to change that to IActionResult
(or Task<IActionResult>
) and the warning is triggered.
The HTTP/1.1 RFC also lists 204 as a valid response:
If a DELETE method is successfully applied, the origin server SHOULD send a 202 (Accepted) status code if the action will likely succeed but has not yet been enacted, a 204 (No Content) status code if the action has been enacted and no further information is to be supplied, or a 200 (OK) status code if the action has been enacted and the response message includes a representation describing the status.
I could only find one case in the official documentation and templates where a Delete
method actually returns some data. That is from an "API Controller with actions, using Entity Framework" scaffolded controller (source code). But I don't think returning the just deleted object makes much sense.
dotnet --info
output:
Laufzeitumgebung:
OS Name: Windows
OS Version: 10.0.16299
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\2.2.105\
Host (useful for support):
Version: 2.2.3
Commit: 6b8ad509b6
.NET Core SDKs installed:
2.1.202 [C:\Program Files\dotnet\sdk]
2.1.505 [C:\Program Files\dotnet\sdk]
2.2.105 [C:\Program Files\dotnet\sdk]
.NET Core runtimes installed:
Microsoft.AspNetCore.All 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.2.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.2.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.2.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
To install additional .NET Core runtimes or SDKs:
https://aka.ms/dotnet-download