I'm working ASP.NET core C# web api where I need to find and prevent the over posting properties in request body.
For example:
Class:
public class MyClass
{
public string Name { get; set; }
public string Department { get; set; }
}
My controller:
[HttpPost()]
public async Task<IActionResult> Post([FromBody]MyClass myClass )
{
...
return something..
}
My request body:
{
"Name" : "Mage",
"Department" : "IT"
"TotalMarks": "445"
}
here I am passing "TotalMarks" property which is not in the model "MyClass". In this case, the JSON formatter simply ignores this value. (The XML formatter does the same.)
But I want to stop and return model validation error, when some properties passed in the API request but those are not the actual model. So is there any common solution for this to support all models.