-
Notifications
You must be signed in to change notification settings - Fork 10.2k
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
EmptyBodyBehavior.Allow should allow a missing Content-Type #36466
Comments
@pranavkm any thoughts on this ? |
EmptyBodyBehavior operates at the formatter level - once a formatter is picked, it decides to allow an empty body. Picking a formatter requires a |
Is there any way to leverage this functionality then? If I was using HttpClient for example to call this action, I would have to newop dummy JsonContent for instance and attach with the body? |
As per this SO answer: Just add content-type in your request header. Without the content-type:application/json will appear 415 when body is empty. |
A workaround until this is resolved might be to create a formatter that does not require a [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public sealed class AllowEmptyBodyAttribute : Attribute
{
}
public class EmptyBodyFormatter : IInputFormatter
{
public bool CanRead(InputFormatterContext context)
{
var endpoint = context.HttpContext.GetEndpoint();
return endpoint?.Metadata.GetMetadata<AllowEmptyBodyAttribute>() is not null;
}
public Task<InputFormatterResult> ReadAsync(InputFormatterContext context)
{
return InputFormatterResult.NoValueAsync();
}
}
[HttpPost]
[AllowEmptyBody]
public IActionResult Action(MyPoco? poco) { ... } Note that this is using a different attribute to allow selection. MVC does not expose EmptyBodyBehavior via ModelMetadata, so we can't query that. |
EmptyBodyBehavior.Allow
allows a 0-length body, but does not work if the request does not have aContent-Type
header. To make bodies truly optional, we need to expand the behavior of the switch to allow a missing Content-Type.Original bug:
Describe the bug
This is either a bug or I'm using this new ASP.NET Core 5.0 feature wrong/API doc is not clear.
To Reproduce
Create new ASP.NET Core Web API from the default template, VS2019, target Framework = .NET 5.0 (Current)
Add below action at the end of the existing WeatherForecastController.cs:
Exceptions (if any)
No exceptions, action is not being hit in point 8 and 10
Further technical details
.NET SDK (reflecting any global.json):
Version: 5.0.301
Commit: ef17233f86
Runtime Environment:
OS Name: Windows
OS Version: 10.0.18363
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\5.0.301\
Host (useful for support):
Version: 5.0.7
Commit: 556582d964
.NET SDKs installed:
2.1.602 [C:\Program Files\dotnet\sdk]
2.1.700 [C:\Program Files\dotnet\sdk]
2.1.801 [C:\Program Files\dotnet\sdk]
2.2.401 [C:\Program Files\dotnet\sdk]
5.0.301 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.All 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.28 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.2.6 [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.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.28 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.16 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.28 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.16 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.16 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Related
#30690
The text was updated successfully, but these errors were encountered: