Four validation programming methods for ASP.net mvc-practical tips

Source: Internet
Author: User

We can use 4 different programming modes for validation of binding parameters.

First, manually verify the binding parameters

When defining the specific action method, it is undoubtedly the most direct way to programmatically validate the parameters that have been successfully bound, and then we demonstrate how to implement the parameter validation logic in the corresponding action method with a simple example. The error message is responded to the client without validation. We defined a person class as the data type being validated in a asp.net MVC application, and its name, gender, and age three attributes represent the name, gender, and ages of one individual.

public class person 
{ 
  [DisplayName (' name ']] public 
  string Name {get; set;} 
 
  [DisplayName ("Gender")] 
  public string Gender {get; set;} 
 
  [DisplayName ("Age")] 
  public int? Age {get; set;} 
} 

Next we define a homecontroller as follows. In the action method index for GET requests, we create a person object and present it as model in the corresponding view. Another index method that supports post requests has an argument of type person, and we call the Validate method to validate the input parameter first in the action method. If the validation succeeds (the Modestate.isvalid property returns True), we return a contentresult that reads "input data is validated", otherwise rendering this parameter as model in the corresponding view.

public class Homecontroller:controller {[HttpGet] public ActionResult Index () {return View ( 
  )); 
 
    [HttpPost] public actionresult Index (person person) {Validate (person); if (! 
    Modelstate.isvalid) {return View (person); 
    else {return Content ("input data passed validation"); } private void Validate (person person) {if (string). IsNullOrEmpty (person. 
    Name) {modelstate.addmodelerror ("name", "' name ' is a required field"); } if (string. IsNullOrEmpty (person. 
    Gender)) {modelstate.addmodelerror ("Gender", "' Gender ' is a required field"); else if (!new string[] {"M", "F"}. Any (g => string.compare. 
    Gender, G, true) = = 0)) {modelstate.addmodelerror ("Gender", "Valid ' Gender ') must be ' M ', ' F '"); } if (null = = person. 
    Age} {Modelstate.addmodelerror ("age", "' age ' is a required field"); else if (person. Age > 25 | | Person. Age < 18) {
      Modelstate.addmodelerror ("Age", "effective ' ages ' must be between the ages of 18-25"); 
 } 
  } 
}

As shown in the previous code snippet, we validate the method by validating the 3 attributes of the person object as a parameter, if the supplied data is not validated, We will call the current Modelstate Addmodelerror method to convert the specified validation error message to Modelerror save. The specific validation rules we use are as follows.

The name, gender, and age attributes of the person object are required fields and cannot be null (or empty strings).
The value of the gender property that represents the gender must be either "M" (Male) or "F" (Female), and the remainder is an invalid value.
Age attribute must be between 18-25 years old.
As shown below is the definition of the action method index corresponding to view, which is a strongly typed view with model type person, which contains a form for editing people information. We call the htmlhelper<tmodel> extension method directly Editorformodel to render the person object as model in the form in edit mode.

@model 
person  
 

After running the program directly, a page for the editor's basic information will be rendered, and if we enter the illegal data and submit it, the corresponding validation information will be presented in the form shown in Figure 1.

Second, the use of validationattribute characteristics

Defining the validation logic and business logic for input parameters in the action method is not a recommended programming method. In most cases, the same data type has the same validation rules in different scenarios, and if we can associate validation rules with data types so that the framework itself enforces data validation, the end developer can focus more on the implementation of the business logic. In fact, this is also the ASP.net MVC model validation system default supported programming method. When we define a data type, we can define the default validation rule by applying the corresponding Validationattribute attribute on the type and its data members.

The "System.ComponentModel.DataAnnotations" namespace defines a series of specific Validationattribute attribute types. Most of them can be applied directly to a property of a custom data type to perform validation on the target data member. These predefined validation features are not the focus of this chapter, and we will give a general introduction to them in the next article.

General validation can be done with these predefined validationattribute features listed above, but in many cases we need to create a custom Validationattribute feature to address some of the special validations. For example, in the validation of the person object in the example above, we require that the gender attribute specify a value that must be one of both "m/m" and "f/f", and that validation will have to be implemented through a custom Validationattribute attribute.

For validation rules such as "a value must be within a specified range", we define a Domainattribute attribute. As the following code fragment shows, Domainattribute has a ienumerable<string> type of read-only property values that provides a list of valid values that are initialized in the constructor. The specific validation implementation in the overridden IsValid method, if the validated value is in this list, is considered successful and returns true. To provide a friendly error message, we rewrote the method Formaterrormessage.

 [AttributeUsage (Attributetargets.property | Attributetargets.field, AllowMultiple = False)] public class Domainattribute:validationattribute {public Ienumera 
 
  Ble<string> Values {get; private set;} Public Domainattribute (String value) {this. 
  Values = new string[] {value}; Public Domainattribute (params string[] values) {this. 
  values = values; 
    public override bool IsValid (object value) {if (null = = value) {return true; return this. Values.any (item => value. 
  ToString () = = Item); Override string Formaterrormessage (string name) {string[] values = this. Values.select (Value => string. Format ("' {0} '", value)). 
    ToArray (); return string. Format (base. Errormessagestring, name,string. 
  Join (",", values)); } 
} 

Because asp.net mvc automatically extracts the Validationattribute attributes that are applied to the target parameter types or data members when the parameter bindings are made, and uses them to validate the data provided, So we no longer need to implement validation in the action method like the example shown above, but only by applying the corresponding Validationattribute attribute when defining the parameter type person to associate the validation rule with the corresponding data member.

The definition of the person type with the associated Validationattribute attribute applied on the attribute member is shown below. We have applied the RequiredAttribute attribute on all three attributes to define them as required data members. The gender and age attributes apply the Domainattribute and Rangeattribute attributes respectively to limit the range of valid property values.

public class person 
{ 
  [DisplayName ("name"]] 
  [Required (errormessageresourcename = "Required",  Errormessageresourcetype = typeof (resources)] public 
  string Name {get; set;} 
 
  [DisplayName ("Gender")] 
  [Required (Errormessageresourcename = "Required",  Errormessageresourcetype = typeof (Resources))] 
  [Domain ("M", "F", "M", "f", Errormessageresourcename = "Domain", Errormessageresourcetype = typeof (Resources))] 
  public string Gender {get; set;} 
 
  [DisplayName ("Age")] 
  [Required (Errormessageresourcename = "Required", Errormessageresourcetype = typeof (Resources))] 
  [Range (Errormessageresourcename = "range", Errormessageresourcetype = typeof (Resources))] 
  public int? Age {get; set;} 
} 

The error messages used by the three Validationattribute features are defined in the project's default resource file, which we can use to create this resource file: Right-click the item in Solution Exploror and select the Properties option on the context menu to open the Project Properties Object box. Finally, select the "Resources" tab page in the dialog box and create a resource file by clicking on the link in the page, as shown in Figure 2.

Since asp.net mvc automatically extracts the Validationattribute attributes that are applied on the binding parameter type to automate validation of the binding's parameters, we do not need to manually validate the parameters in a specific action method. As shown in the following code snippet, we no longer explicitly invoke the Validate method in the action method index, but we still get the output as shown in Figure 1 when we run the program and submit the form without entering the illegal data.

public class Homecontroller:controller 
{ 
  //other member 
  [HttpPost] public 
  actionresult Index (People person) 
  { 
    if (! Modelstate.isvalid) 
    {return 
      View (person); 
    } 
    else 
    {return 
      Content ("Enter data through Validation"); 
    } 
  } 
 

Third, let data type implement Ivalidatableobject interface

In addition to defining the validation rules directly on the data type through the Validationattribute attribute and allowing asp.net mvc to validate the parameters based on the parameter bindings, we can also define the validation operations directly in the data type. Now that we've implemented the validation operations directly on the data type, which means that the corresponding data objects have the ability to "authenticate themselves", let's call these data types "self-validating types." These types of authentication are implemented with interface ivalidatableobject that are defined under the "System.ComponentModel.DataAnnotations" namespace.

Public interface Ivalidatableobject 
{ 
  ienumerable<validationresult> Validate (validationcontext Validationcontext); 
} 

As the preceding code fragment shows, the Ivalidatableobject interface has a unique method validate, and validation for itself is implemented in the method. For the data type person defined in the above demo instance, we can define it as a self validating type as follows.

public class Person:ivalidatableobject {[DisplayName (' name ')] public string Name {get; set;} 
 
  [DisplayName ("Gender")] public string Gender {get; set;} [DisplayName ("Age")] public int? 
 
  Age {get; set;} Public ienumerable<validationresult> Validate (Validationcontext validationcontext) {Person person = Valida 
    Tioncontext.objectinstance as person; 
    if (null = = person) {yield break; } if (string. IsNullOrEmpty (person. 
    Name) {yield return new Validationresult ("' Name is a required field", New string[]{"name"}); } if (string. IsNullOrEmpty (person. 
    Gender)) {yield return new Validationresult ("' Gender ' is a required field", new string[] {"Gender"}); else if (!new string[]{"M", "F"}. Any (g=>string.compare. Gender,g, true) = = 0) {yield return new Validationresult ("Valid ' Gender ' must be ' M ', ' F '", new string[] {"Gender" 
    }); } if (null = = person. Age) {yield return new ValIdationresult ("' age ' is a required field", new string[] {"Age"}); else if (person. Age > 25 | | Person. 
    Age < = {yield return to new Validationresult ("' age ' must be between 18-25 years old", new string[] {"Aged"}); 
 }       
  } 
}

As shown in the code snippet above, we have the person type implement the Ivalidatableobject interface. In the implementation of the Validate method, we obtain the authenticated person object from the validation context and authenticate its property members individually. If the data member does not pass validation, we encapsulate the error message and the data member name (property name) with a Validationresult object, which ultimately returns a collection of element type Validationresult. Without making any changes to any other code, we run the program directly and submit the form after we have entered the illegal data, and we still get the output shown in Figure 1.

Four, let the data type implement IDataErrorInfo interface

Above we let the data type implement the Ivalidatableobject interface and define the specific validation logic in the implemented validate method, such that the type can be recognized by ASP.net MVC, which automatically invokes the method to validate the bound data object. Similar automated validation results can be achieved if we let the data type implement the IDataErrorInfo interface.

The IDataErrorInfo interface is defined under the "System.ComponentModel" namespace, which provides a standard way of customizing error information. As shown in the following code fragment, IDataErrorInfo has two members, the read-only property error is used to get an error message based on itself, and the read-only index is used to return the error message for the specified data member.

Public interface IDataErrorInfo 
{ 
  string Error {get;} 
  String this[string columnName] {get;} 
} 

The

is also for the example shown above, and now we have redefined the data type person who needs to be validated. As shown in the following code snippet, we have the person implement the IDataErrorInfo interface. In the implemented index, we treat the index parameter ColumnName as a property name, validate against the corresponding property member according to the above rules, and return the appropriate error message if the validation fails. Without making any changes to any other code, we run the program directly and submit the form after we have entered the illegal data, and we still get the output shown in Figure 1.

public class Person:idataerrorinfo {[DisplayName (' name ')] public string Name {get; set;} 
 
  [DisplayName ("Gender")] public string Gender {get; set;} [DisplayName ("Age")] public int? 
 
  Age {get; set;} 
 
  [Scaffoldcolumn (false)] public string Error {get; private set;} 
          public string this[string ColumnName] {get {switch (columnName) {case ' Name ': {if (string. IsNullOrEmpty (this. 
            Name) {return "' name ' is a required field '; 
          return null; Case "Gender": {if (string). IsNullOrEmpty (this. 
            Gender)) {return "' sex ' is a required field"; else if (!new string[] {"M", "F"}. Any (g => string.compare) (this. 
            Gender, G, true) = = 0)) {return ' ' sex ' must be ' M ', ' F ' one '; 
          return null; 
       Case ' age ':   {if (null = = this. 
            Age) {return "' ages ' is a required field"; else if (this. Age > 25 | | This. 
            Age </) {return "' ages ' must be between 18-25 years old"; 
          return null; 
             
      } Default:return null; 
 } 
    } 
  } 
}

The above is to use 4 different programming mode for binding parameter verification of the implementation code, I hope to help you learn.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.