问题
I'm using asp.net web api. I have a method in UserController which receives POST-requests:void Post([FromBoby] UserInfo userInfo)
I have a model UserInfo with fields: Login, Name.
I can post json: {Login:"someLogin", Name:"someName"} and it will successfully mapped to userInfo parameter.
I can also post json: {Login:"someLogin", Name:"someName", UnwantedParameter:"someString"} which will also ssuccessfully mapped to UserInfo parameter and UnwantedParameter will be ignored.
How to handle 'unwanted' parameters to throw response exception?
回答1:
Take a look at this question, I believe you are asking the same thing:
POST object in .NET WebAPI
At the end of the day, they suggest setting the following property (typically in the Global.asax.cs)
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.MissingMemberHandling = MissingMemberHandling.Error;
来源:https://stackoverflow.com/questions/16654295/asp-net-web-api-unwanted-parameters-in-request