Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Optional body parameters no longer work since upgrading to .NET Core 2.0 #6920

Closed
ben-foster-cko opened this issue Oct 6, 2017 · 2 comments

Comments

@ben-foster-cko
Copy link

I have an action filter that automatically checks ModelState and returns 400 if it's not valid.

We have a few actions that have optional body parameters, for example:

        // POST api/values
        [HttpPost]
        public IActionResult Post([FromBody]SomeClass request = null)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            return Ok();
        }

Specifying the default value of null used to be enough to mark them as optional. Since upgrading to ASP.NET Core 2.0 I get the following response:

{
  "": [
    "A non-empty request body is required."
  ]
}
@ben-foster-cko
Copy link
Author

It looks like the null handling behavior was changed in this PR.

There is a new option AllowEmptyInputInBodyModelBinding that is false by default. If you want to handle the null values yourself you'll need to set this to true.

@hmvs
Copy link

hmvs commented Feb 7, 2018

Would be nice to have something like [FromBody(AllowNull=true)] instead of having this behavior for all the methods.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants