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?
Add a comment
|
1 Answer
Take a look at this question, I believe you are asking the same thing:
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;
3 Comments
wakeup
There is a new problem. JSON: {Login:"someLogin",Name:"someName"} successfully maped. JSON: {login:"someLogin",name:"someName"} could not be mapped. This problem started after setting MissingMemberHandling.Error. So now works only upper case json-input :(
wakeup
Maybe there is a way to receive both camelCase and PascalCase JSON input?
BlakeH
try this GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();