The model State and data annotations in C # provide us with very convenient validation of the request data.
1. Modelstate
Modelstate is useful when it comes to validating data:
1) Verify the data, and save the error message corresponding to the data.
2) Microsoft's Dry (Don ' t Repeat yourself) design, through Modelstate can do server-side validation, while cooperating with jquery validation to generate front-end data validation
2. Data Annotations, data validation
If we want to add data validation, we'll start by creating our data validation entities.
For example, we have a way to save the user information, and the input entity is saveuserinfoinput
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel.DataAnnotations;usingSystem.Linq;usingsystem.web;namespacefeidu.models.usermodels{/// <summary> ///Save user Information/// </summary> Public classSaveuserinfoinput {/// <summary> ///name/// </summary>[Required (errormessage ="Please enter a name")] Public stringName {Get;Set; } /// <summary> ///Mobile phone number/// </summary>[Required (errormessage ="Please enter your mobile phone number")] [RegularExpression (@"^1[1|2|3|4|5|6|7|8| 9][0-9]\d{8}$", errormessage ="Phone number format error")] Public stringPhone {Get;Set; } /// <summary> ///Province/// </summary>[Required (errormessage ="Please enter the province")] Public stringProvince {Get;Set; } /// <summary> ///City/// </summary>[Required (errormessage ="Please enter the city")] Public stringCity {Get;Set; } /// <summary> ///Address/// </summary>[Required (errormessage ="Please enter an address")] Public stringAddress {Get;Set; } /// <summary> ///mobile phone Verification Code/// </summary>[Required] Public stringPhonecode {Get;Set; } }}
As you can see from the above, we have added annotations for data validation, Required, RegularExpression
Then look at the method of saving user information in our controller:
Public Responsemessageobj saveuserinfo (Saveuserinfoinput input)
When we pass the data, we find that the input validation class saveuserinfoinput is not used because we have not yet joined Modelstate's validation.
3. Validation of Modelstate to join the global
First we build a class Globalactionfilterattribute, inheriting ActionFilterAttribute, as follows:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Net;usingSystem.Net.Http;usingsystem.web;usingSystem.Web.Http.Controllers;usingSystem.Web.Http.Filters;namespacefeidu.common{/// <summary> ///Verification Information/// </summary> Public classGlobalactionfilterattribute:actionfilterattribute { Public Override voidonactionexecuting (Httpactioncontext actioncontext) {if(ActionContext.ModelState.IsValid = =false) {Actioncontext.response=actionContext.Request.CreateErrorResponse (Httpstatuscode.badrequest, actioncontext.modelstate); } } }}
Then, in the Register method of the Webapiconfig class, register the global FilterAttribute
// Register the global Filterconfig. Filters.add (new Globalactionfilterattribute ());
Then we run it and find out that the validation of the data annotations is possible.
Of course, we can also add data validation to a fixed method without registering global data validation.
4. Data Annotations Demo
1) non-null validation [Required]
2) Length verification [stringlength (minimumlength = 10)]
3) Regular Expression validation [RegularExpression ("Your expression")]
4) value range validation [range (10, 100)]
5) comparison verification [Compare ("Name")]
C # WEB data annotations Data Annotations, model state modelstate, validation