Skip to content

Optional [FromBody] Model Binding is not working #29570

@toothache

Description

@toothache

Describe the bug

I am trying to use optional [FromBody] model binding, for example,

[HttpPost("optional")]
public IActionResult PostOptional(WeatherForecast forecast = null)
{
    return Ok(forecast != null);
}

MVC options are configured as below to allow empty input in body:

services.AddControllers(options =>
{
    options.AllowEmptyInputInBodyModelBinding = true;
});

However, when requesting the API with an empty request body, I got the Error 415,

> curl --request POST -k -i 'https://localhost:5001/weatherforecast/optional' -d ''
HTTP/1.1 415 Unsupported Media Type
Date: Sun, 24 Jan 2021 05:23:26 GMT
Content-Type: application/problem+json; charset=utf-8
Server: Kestrel
Transfer-Encoding: chunked

{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.13","title":"Unsupported Media Type","status":415,"traceId":"00-a2d12c1ee6e2844e9542de749df73d7d-b99cdaa84c27cd44-00"}%  (base) ➜  examples git:(yatengh/refactor_pages) ✗

To Reproduce

I'm using the sample code from https://github.com/pranavkm/OptionalBodyBinding. This is a sample code given from #6878.

Besides, I also targeted the project to netcore5. The bug still repos.

Exceptions (if any)

Further technical details

  • ASP.NET Core version: 5.0.102
  • Include the output of dotnet --info:
    .NET SDK (reflecting any global.json):
     Version:   5.0.102
     Commit:    71365b4d42
    
    Runtime Environment:
     OS Name:     Mac OS X
     OS Version:  10.13
     OS Platform: Darwin
     RID:         osx.10.13-x64
     Base Path:   /usr/local/share/dotnet/sdk/5.0.102/
    
    Host (useful for support):
      Version: 5.0.2
      Commit:  cb5f173b96
    
    .NET SDKs installed:
      2.1.400 [/usr/local/share/dotnet/sdk]
      3.1.405 [/usr/local/share/dotnet/sdk]
      5.0.102 [/usr/local/share/dotnet/sdk]
    
    .NET runtimes installed:
      Microsoft.AspNetCore.All 2.1.2 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
      Microsoft.AspNetCore.App 2.1.2 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
      Microsoft.AspNetCore.App 3.1.11 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
      Microsoft.AspNetCore.App 5.0.2 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
      Microsoft.NETCore.App 2.1.2 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
      Microsoft.NETCore.App 3.1.11 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
      Microsoft.NETCore.App 5.0.2 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
    
    To install additional .NET runtimes or SDKs:
      https://aka.ms/dotnet-download
    
  • The IDE (VS / VS Code/ VS4Mac) you're running on, and its version
    From console.

Metadata

Metadata

Assignees

Labels

Priority:1Work that is critical for the release, but we could probably ship withoutfeature-model-bindingold-area-web-frameworks-do-not-use*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labelstriage-focusAdd this label to flag the issue for focus at triage

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions