1

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 Answer 1

4

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;
Sign up to request clarification or add additional context in comments.

3 Comments

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 :(
Maybe there is a way to receive both camelCase and PascalCase JSON input?
try this GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.