MVC下ASP.NET的表单验证实现

开发 后端
MVC下实现表单验证可以使用多种方法。国外已经出现了一些Mvc下使用的验证框架。本文中提供的方法只是众多方法之一,可以供学习者参考一二。

在Web开发中,表单提交算是一种很常见的从客户端获取数据的方式了。然而,用户的行为永远都是无法预料的。为此,我们在程序中不得已必须对用户输入的数据进行严格效验。在WebForm时代我们常用的手段是验证控件,但是到了Mvc时代,再使用控件变得困难了,因此我们必须找到新的方式来解决这个问题。

在实际使用中,我们可以考虑多种形式来进行这一验证(注:本文目前只研究服务器端验证的情况),最直接的方式莫过于对每个表单值手动用C#代码进行验证了,比如:

if(!Int32.TryParse(Request.Form[“age”], out age)){
xxxx…
}
If(age < xxx || age > xxx){
xxxx…
}

 

然而正如上面看到的一样,这种方式枯燥而繁琐,需要用户对每个字段都要手动效验,或许开发人员的一不小心就会造成系统的漏洞。因此,制造出一个能对这种行为进行自动进行的轮子势在必行,当然,到本文写作的时候为止,国外已经出现了一些Mvc下使用的验证框架,然而天下轮子不怕多,我在此厚颜再造出个,只希望不被冠上山寨之名。

该框架的缔造源自4MVC团队的Infancy项目,去年年底开始这个项目的时候,正是mvc框架加入ModelBinder的时候,当时便想到了通过使用ModelBinder来实现一种服务器端自动验证框架,经过多次修改,该框架慢慢实现了我需要的功能,本系列文章将再次回顾该过程,将该框架的一步步的实现过程加以更细致的重现。

下面正式开始框架的开发,首先我们明确下我们的基本需求:

1.该框架针对简单实体类(POCO)

2.该框架能自动对实体类的属性进行效验

3.该实体能被ModelBinder使用

4.能方便或者自动的执行该效验,并取得效验结果和信息

为了实现上面的目标,我们首先来确定一些需要使用的技术手段:

1.要能访问任意POCO的属性,必然用到反射

2.要能对属性进行限制,可选择使用XML或者Attribute,对程序员来说,Attribute远比XML来的方便和友好,因此选择Attribute

3.实现实体验证方法,可能会使用Command模式,也可能不需要

下面开始我们的实践了,首先我们考虑测试代码,假设我拥有实体Student,Student拥有属性Source,要求Source是int类型,且范围为0-100,那么测试代码的模式应该如下:

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

 

也就是说,我们需要在一个验证方法中对该对象的所有属性进行验证,那么我们考虑对系统各部分的构建,首先我们需要一个RangeAttribute,这个类能包含对属性的验证信息,大致如下:

public class RangeAttribute : Attribute{
public int Mix{ get; set; } //范围下限
public int Max{ get; set; } //范围上限
public string Message{ get; set;} //出错信息

public RangeAttribute(int min, int max, string message){
Min = min;
Max = max;
Message = message;
}
}


这样一来我们的Student就可以如此构造

public class Student{
[Range(0, 100, “分数的范围必须在{0}和{1}之间.”)]
public int Source{ get; set; }
}


然而,这样仅仅是个花架子,在默认情况下这个Range没有起到任何作用,除了程序员看到代码之后知道了Source有这样的限制要求,那么,我们需要如何将这个Attribute和验证结合起来呢?自然就是反射。

#p#

我们在Student中实现如下方法:

public bool Validate(){
bool result = true;
Type type = this.GetType();
PropertyInfo property = type.GetProperty(“Source”); //获取Source属性
RangeAttribute [] attributes =
property.GetCustomAttributes(typeof(Attribute), true)
as RangeAttribute []; //获取Range属性集合
//遍历集合对属性进行验证
foreach(var item in attribute){
int value = (int)property.GetValue(this, null);
if(value < item.Min || value > item.Max){
result = false;
}
}
return result;
}


那么再回过头看先前的测试,我们可以发现,测试成功运行了(相关代码见附带项目的Leven.Validate01和test项目Validate01Test.cs).

我们在看目前的代码,现在我们能测试Source,如果我们的Student类中还有一项Age,范围为6-150呢,那么Student中加上如下代码:

[Range(6, 150, "学生年龄必须在{0}和{1}之间.")]
public int Age { get; set; }
那么我们的Validate方法是否能正确验证呢?为了验证结果,我们重新编写测试:

[TestMethod()]
public void ValidateTest() {
Student student = new Student() {
Source = 80,
Age = 0
};
bool validateResult = student.Validate();
Assert.IsFalse(validateResult);
student.
validateResult = student.Validate();
}


执行测试,很遗憾,测试无法通过了.我们可以再看看Validate方法,可以发现,其中只对Source属性进行了验证,那么我们可以想办法修改代码,让其能对Age和Source方法同时验证。

public bool Validate() {
bool result = true;
Type type = this.GetType();
PropertyInfo[] properties = type.GetProperties();
foreach (var property in properties) {
RangeAttribute[] attributes =
property.GetCustomAttributes(typeof(RangeAttribute), true) as RangeAttribute[];
foreach (var item in attributes) {
int value = (int)property.GetValue(this, null);
if (value < item.Min || value > item.Max) {
result = false;
}
}
}
return result;
}


修改过的方法中将遍历所有的属性,然后进行验证,这时候再次运行测试,生动的绿色代表我们重新获得了成功。

下面我们再次考虑新的可能情况,如果Student需要一个Name属性,这是一个必须字段.我们考虑新增一个RequiredAttribute来实现该功能,该部分代码如下(参见项目Leven.Validate03):

[AttributeUsage(AttributeTargets.Property)]
public class RequiredAttribute : Attribute {
public bool IsRequired { get; set; }
public string Message { get; set; }
public RequiredAttribute(string message) {
IsRequired = true;
Message = message;
}
}


然后修改Student部分,新增下面部分:

[Required]
public string Name { get; set; }
再次修改测试代码:

[TestMethod()]
public void ValidateTest() {
Student student = new Student() {
Age = 20,
Source = 89,
Name = string.Empty
};
bool validateResult = student.Validate();
Assert.IsFalse(validateResult);
}


执行测试,结果失败了.查看原因,显然可以看到,是Validate方法中

RangeAttribute[] attributes =property.GetCustomAttributes(typeof(RangeAttribute), true) as RangeAttribute[];

只验证了RangeAttribute,那针对我们加入的RequiredAttribute自然是无能为力了,为了能验证RequiredAttribute,我们再次修改了代码:

public bool Validate() {
bool result = true;
bool requiredResult = true;
Type type = this.GetType();
PropertyInfo[] properties = type.GetProperties();
foreach (var property in properties) {
RangeAttribute[] attributes =
property.GetCustomAttributes(typeof(RangeAttribute), true)
as RangeAttribute[]; //获取RangeAttribute集合
RequiredAttribute[] requiredAttributes =
property.GetCustomAttributes(typeof(RequiredAttribute), true)
as RequiredAttribute[]; //获取RequiredAttribute集合
//遍历RangeAttribute进行验证
foreach (var item in attributes) {
int value = (int)property.GetValue(this, null);
if (value < item.Min || value > item.Max) {
result = false;
}
}
//遍历RequiredAttr进行验证
foreach (var item in requiredAttributes) {
object value = property.GetValue(this, null);
if (value is string) {
if (String.IsNullOrEmpty((value as string))) {
requiredResult = false;
}
} else {
if (value == null) {
requiredResult = false;
}
}
}
}
return result && requiredResult;
}


这次的代码量增加了不少,不过经过我们的不懈努力,测试再一次通过了,但是,我们再次回来查看验证部分的代码,不难发现一个问题,每次我们新增了验证Attribute之后都必须手动在Validate方法中增加响应的代码,目前我们还只有两个Attribute,如果一个系统中有20甚至200个Attribute(当然只是打个比方),该方法的长度恐怕将是个恐怖的数字,这样的方法势必无比丑陋。

【编辑推荐】

  1. ASP.Net MVC框架配置与分析
  2. ASP.net中用axWebBrowser中提交表单
  3. MVC详解 什么是真正的"框架"
责任编辑:yangsai 来源: ITPUB论坛
相关推荐

2009-07-29 16:47:40

ASP.NET表单身份

2009-07-20 15:44:32

ASP.NET MVC

2014-04-09 10:18:21

ASP.NETMVC

2009-09-18 10:20:26

PRG数据验证

2009-07-28 14:47:18

ASP.NET MVC

2011-04-12 13:53:25

ASP.NET MVCjQuery

2009-07-29 17:23:17

ASP.NET表单

2009-07-23 16:59:31

ASP.NET认证Form表单

2009-09-10 09:50:47

ASP.NET MVC

2009-07-24 13:20:44

MVC框架ASP.NET

2009-07-31 12:43:59

ASP.NET MVC

2009-06-01 10:23:31

asp.net mvcasp.net mvc.net mvc框架

2009-07-22 16:02:39

ASP.NET MVCPagedList

2009-11-24 15:11:21

ASP.NET MVC

2009-07-29 09:59:10

ASP.NET For

2009-07-22 18:07:55

论坛应用程序ASP.NET MVC

2009-08-05 18:22:55

2009-07-22 10:34:37

ActionInvokASP.NET MVC

2009-07-22 09:11:02

Action方法ASP.NET MVC

2009-12-21 10:05:10

ASP.NET MVC
点赞
收藏

51CTO技术栈公众号