We all know that client-side validation by validating a page without having to post its content is what we expect from a web app these days. However, this may not be sufficient—for example for the (granted, few) cases where JavaScript is disabled. In this case, we need to ensure we validate our data on the server-side before actually doing anything with it. ASP.NET Core supports both scenarios; let's see how.
Server-side validation
The result of validating a submitted model (normally through POST) is always available in the ModelState property of the ControllerBase class, and it is also present in the ActionContext class. Consider the following code snippet:
if (!this.ModelState.IsValid)
{
if (this.ModelState["Email"].Errors.Any())
{
var emailErrors = string.
Join(Environment.NewLine, this.ModelState
["Email"].Errors.Select(e => e.ErrorMessage));
}
}
As you can...