Form Verification Implementation of ASP. NET under MVC (1)

Source: Internet
Author: User

In Web development, form submission is a common way to obtain data from the client. However, user behavior is always unpredictable. Therefore, we have to strictly verify the user input data in the program. In the WebForm era, we often use verification controls. However, in the Mvc era, it becomes difficult to use controls. Therefore, we must find a new way to solve this problem.
In actual use, we can consider multiple forms for this verification (Note: This article only studies server-side verification ), the most direct method is to manually use the C # code to verify each form value. For example:

If (! Int32.TryParse (Request. Form ["age"], out age )){
Xxxx...
}
If (age <xxx | age> xxx ){
Xxxx...
}

However, as we can see above, this method is boring and cumbersome, requiring users to manually verify each field. A developer's carelessness may cause system vulnerabilities. Therefore, it is imperative to create a wheel that can automatically perform such behaviors. By the time of writing this article, some verification frameworks used in Mvc have already appeared in foreign countries, however, there are not many wheels in the world. I am here to make a new one, but I only hope that I will not be named as a shanzhai.

This framework was created from the Infancy project of the 4 mvc team. When this project was started at the end of last year, it was just when the mvc framework was added to ModelBinder, at that time, I thought of using ModelBinder to implement a server-side automatic verification framework. After several modifications, the Framework gradually implemented the functions I needed, this series of articles will review this process and reproduce the implementation process of this framework in more detail.

The following is the formal start of framework development. First, we need to clarify our basic needs:

1. This framework targets simple entity classes (POCO)

2. This framework can automatically validate the attributes of the object class.

3. This object can be used by ModelBinder.

4. You can conveniently or automatically perform the verification and obtain the verification results and information.

In order to achieve the above goal, we should first determine some technical means to be used:

1. Reflection is necessary to access any POCO attributes.
2. to restrict attributes, you can choose to use XML or Attribute. For programmers, Attribute is far more convenient and friendly than XML. Therefore, select Attribute.

3. Implementing Entity verification methods may use the Command mode or

Let's start with our practice. First, let's consider the test code. Assume that I have the entity Student and Student have the Attribute Source. The Source must be of the int type and the value range is 0-100, the test code mode should be as follows:

Student student = new Student (){
Source =-1
};
Bool validateResult = student. Validate ();
Assert. IsFalse (validateResult );

That is to say, we need to verify all the attributes of the object in a verification method, so we need a RangeAttribute first to consider the construction of each part of the system, this class can contain information about property verification, which is roughly as follows:

Public class RangeAttribute: Attribute {
Public int Mix {get; set;} // lower limit of the range
Public int Max {get; set;} // maximum range
Public string Message {get; set;} // error Message
Public RangeAttribute (int min, int max, string message ){
Min = min;
Max = max;
Message = message;
}
}


In this way, our Student can be constructed in this way.

Public class Student {
[Range (0,100, "the score Range must be between {0} and {1}.")]
Public int Source {get; set ;}
}


However, this is just a fancy. By default, this Range does not play any role. Except that the programmer knows that the Source has such restrictions after seeing the code, how do we combine this Attribute with verification? Naturally, reflection.


Related Article

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.