In WEBAPi, you can use attributes from System.ComponentModel.DataAnnotations namespace to set validation rules for properties on your models.
use ModelState.IsValid to validate the model.
Under-Posting – Client posting data that are incomplete, e.g.,
(It’s missing Price and Weight)
But since they’re not required, so JSON formatter automatically set their default to 0.
If you want to distinguish 0 and NULL, do this:
(1) Make the property [Required]
(2) Make the property nullable
Over-Posting – Client posting more data than you expected, such as:
JSON simply ignores the value.
If there is a property in your model that you don’t want anyone to change/update, then create a DTO and serve it to the client. such as:
Handling Validation errors
Use action filter to catch invalidate ModelState
If model state invalid, it send a HTTP response (action is not invoked)