How to make Web API throw error when receiving unmapped field in json request

I am creating a REST API using the Web API. The problem I am facing is that the JSON serializer does not reject non-displayable fields. Assuming I have a simple object:

public class MyClass
{
 public bool MyBool { get; set; }
 public string MyString { get; set; }
}

And I have a simple controller that takes an object of this type in the request body

public void Post(MyClass instace)
{
   ...
}

Now I am sending a request to the endpoint of this controller with the following JSON in the request body:

{"MyBool": true, "MyString": "Really", "InvalidField": "Invalid"}

, true MyBool "Valid" MyString, InvalidField. , , ?

+3
2
+3

Required :

public class MyClass
{
 [Required]
 public bool MyBool { get; set; }

 [Required]
 public string MyString { get; set; }
}

. http://msdn.microsoft.com/library/system.componentmodel.dataannotations.requiredattribute.aspx

0

All Articles