In ASP .NET WebAPI, consider the following simple model class:
public class Model {
public string Value { get; set; }
}
If I have a controller method that takes this as a parameter:
[HttpPut]
public HttpResponseMessage PutModel(Model data)
{
if (!ModelState.IsValid)
return Request.CreateResponse(HttpStatusCode.BadRequest);
// more code ...
}
and I pass in { "unknown": "value" }
as the request body, the model binder will gleefully succeed, completely ignoring the unknown field. This is not very acceptable for future-proofing, since we want to reserve all possible field values and error out if they are provided rather than just silently fail, allowing someone to shoot themselves in the foot later on.
How do I make the model binding error on unknown fields?