MVC下ASP.NET的表单验证实现
创始人
2024-03-24 15:52:16
0

在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详解 什么是真正的"框架"

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...