ASP.NET WEB API, unwanted parameters in request

被刻印的时光 ゝ 提交于 2020-01-06 02:20:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!