自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

MVC下ASP.NET的表單驗(yàn)證實(shí)現(xiàn)

開(kāi)發(fā) 后端
MVC下實(shí)現(xiàn)表單驗(yàn)證可以使用多種方法。國(guó)外已經(jīng)出現(xiàn)了一些Mvc下使用的驗(yàn)證框架。本文中提供的方法只是眾多方法之一,可以供學(xué)習(xí)者參考一二。

在Web開(kāi)發(fā)中,表單提交算是一種很常見(jiàn)的從客戶(hù)端獲取數(shù)據(jù)的方式了。然而,用戶(hù)的行為永遠(yuǎn)都是無(wú)法預(yù)料的。為此,我們?cè)诔绦蛑胁坏靡驯仨殞?duì)用戶(hù)輸入的數(shù)據(jù)進(jìn)行嚴(yán)格效驗(yàn)。在WebForm時(shí)代我們常用的手段是驗(yàn)證控件,但是到了Mvc時(shí)代,再使用控件變得困難了,因此我們必須找到新的方式來(lái)解決這個(gè)問(wèn)題。

在實(shí)際使用中,我們可以考慮多種形式來(lái)進(jìn)行這一驗(yàn)證(注:本文目前只研究服務(wù)器端驗(yàn)證的情況),最直接的方式莫過(guò)于對(duì)每個(gè)表單值手動(dòng)用C#代碼進(jìn)行驗(yàn)證了,比如:

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

 

然而正如上面看到的一樣,這種方式枯燥而繁瑣,需要用戶(hù)對(duì)每個(gè)字段都要手動(dòng)效驗(yàn),或許開(kāi)發(fā)人員的一不小心就會(huì)造成系統(tǒng)的漏洞。因此,制造出一個(gè)能對(duì)這種行為進(jìn)行自動(dòng)進(jìn)行的輪子勢(shì)在必行,當(dāng)然,到本文寫(xiě)作的時(shí)候?yàn)橹梗瑖?guó)外已經(jīng)出現(xiàn)了一些Mvc下使用的驗(yàn)證框架,然而天下輪子不怕多,我在此厚顏再造出個(gè),只希望不被冠上山寨之名。

該框架的締造源自4MVC團(tuán)隊(duì)的Infancy項(xiàng)目,去年年底開(kāi)始這個(gè)項(xiàng)目的時(shí)候,正是mvc框架加入ModelBinder的時(shí)候,當(dāng)時(shí)便想到了通過(guò)使用ModelBinder來(lái)實(shí)現(xiàn)一種服務(wù)器端自動(dòng)驗(yàn)證框架,經(jīng)過(guò)多次修改,該框架慢慢實(shí)現(xiàn)了我需要的功能,本系列文章將再次回顧該過(guò)程,將該框架的一步步的實(shí)現(xiàn)過(guò)程加以更細(xì)致的重現(xiàn)。

下面正式開(kāi)始框架的開(kāi)發(fā),首先我們明確下我們的基本需求:

1.該框架針對(duì)簡(jiǎn)單實(shí)體類(lèi)(POCO)

2.該框架能自動(dòng)對(duì)實(shí)體類(lèi)的屬性進(jìn)行效驗(yàn)

3.該實(shí)體能被ModelBinder使用

4.能方便或者自動(dòng)的執(zhí)行該效驗(yàn),并取得效驗(yàn)結(jié)果和信息

為了實(shí)現(xiàn)上面的目標(biāo),我們首先來(lái)確定一些需要使用的技術(shù)手段:

1.要能訪問(wèn)任意POCO的屬性,必然用到反射

2.要能對(duì)屬性進(jìn)行限制,可選擇使用XML或者Attribute,對(duì)程序員來(lái)說(shuō),Attribute遠(yuǎn)比XML來(lái)的方便和友好,因此選擇Attribute

3.實(shí)現(xiàn)實(shí)體驗(yàn)證方法,可能會(huì)使用Command模式,也可能不需要

下面開(kāi)始我們的實(shí)踐了,首先我們考慮測(cè)試代碼,假設(shè)我擁有實(shí)體Student,Student擁有屬性Source,要求Source是int類(lèi)型,且范圍為0-100,那么測(cè)試代碼的模式應(yīng)該如下:

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

 

也就是說(shuō),我們需要在一個(gè)驗(yàn)證方法中對(duì)該對(duì)象的所有屬性進(jìn)行驗(yàn)證,那么我們考慮對(duì)系統(tǒng)各部分的構(gòu)建,首先我們需要一個(gè)RangeAttribute,這個(gè)類(lèi)能包含對(duì)屬性的驗(yàn)證信息,大致如下:

public class RangeAttribute : Attribute{
public int Mix{ get; set; } //范圍下限
public int Max{ get; set; } //范圍上限
public string Message{ get; set;} //出錯(cuò)信息

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


這樣一來(lái)我們的Student就可以如此構(gòu)造

public class Student{
[Range(0, 100, “分?jǐn)?shù)的范圍必須在{0}和{1}之間.”)]
public int Source{ get; set; }
}


然而,這樣僅僅是個(gè)花架子,在默認(rèn)情況下這個(gè)Range沒(méi)有起到任何作用,除了程序員看到代碼之后知道了Source有這樣的限制要求,那么,我們需要如何將這個(gè)Attribute和驗(yàn)證結(jié)合起來(lái)呢?自然就是反射。

#p#

我們?cè)赟tudent中實(shí)現(xiàn)如下方法:

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屬性集合
//遍歷集合對(duì)屬性進(jìn)行驗(yàn)證
foreach(var item in attribute){
int value = (int)property.GetValue(this, null);
if(value < item.Min || value > item.Max){
result = false;
}
}
return result;
}


那么再回過(guò)頭看先前的測(cè)試,我們可以發(fā)現(xiàn),測(cè)試成功運(yùn)行了(相關(guān)代碼見(jiàn)附帶項(xiàng)目的Leven.Validate01和test項(xiàng)目Validate01Test.cs).

我們?cè)诳茨壳暗拇a,現(xiàn)在我們能測(cè)試Source,如果我們的Student類(lèi)中還有一項(xiàng)Age,范圍為6-150呢,那么Student中加上如下代碼:

[Range(6, 150, "學(xué)生年齡必須在{0}和{1}之間.")]
public int Age { get; set; }
那么我們的Validate方法是否能正確驗(yàn)證呢?為了驗(yàn)證結(jié)果,我們重新編寫(xiě)測(cè)試:

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


執(zhí)行測(cè)試,很遺憾,測(cè)試無(wú)法通過(guò)了.我們可以再看看Validate方法,可以發(fā)現(xiàn),其中只對(duì)Source屬性進(jìn)行了驗(yàn)證,那么我們可以想辦法修改代碼,讓其能對(duì)Age和Source方法同時(shí)驗(yàn)證。

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;
}


修改過(guò)的方法中將遍歷所有的屬性,然后進(jìn)行驗(yàn)證,這時(shí)候再次運(yùn)行測(cè)試,生動(dòng)的綠色代表我們重新獲得了成功。

下面我們?cè)俅慰紤]新的可能情況,如果Student需要一個(gè)Name屬性,這是一個(gè)必須字段.我們考慮新增一個(gè)RequiredAttribute來(lái)實(shí)現(xiàn)該功能,該部分代碼如下(參見(jiàn)項(xiàng)目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; }
再次修改測(cè)試代碼:

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


執(zhí)行測(cè)試,結(jié)果失敗了.查看原因,顯然可以看到,是Validate方法中

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

只驗(yàn)證了RangeAttribute,那針對(duì)我們加入的RequiredAttribute自然是無(wú)能為力了,為了能驗(yàn)證RequiredAttribute,我們?cè)俅涡薷牧舜a:

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進(jìn)行驗(yàn)證
foreach (var item in attributes) {
int value = (int)property.GetValue(this, null);
if (value < item.Min || value > item.Max) {
result = false;
}
}
//遍歷RequiredAttr進(jìn)行驗(yàn)證
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;
}


這次的代碼量增加了不少,不過(guò)經(jīng)過(guò)我們的不懈努力,測(cè)試再一次通過(guò)了,但是,我們?cè)俅位貋?lái)查看驗(yàn)證部分的代碼,不難發(fā)現(xiàn)一個(gè)問(wèn)題,每次我們新增了驗(yàn)證Attribute之后都必須手動(dòng)在Validate方法中增加響應(yīng)的代碼,目前我們還只有兩個(gè)Attribute,如果一個(gè)系統(tǒng)中有20甚至200個(gè)Attribute(當(dāng)然只是打個(gè)比方),該方法的長(zhǎng)度恐怕將是個(gè)恐怖的數(shù)字,這樣的方法勢(shì)必?zé)o比丑陋。

【編輯推薦】

  1. ASP.Net MVC框架配置與分析
  2. ASP.net中用axWebBrowser中提交表單
  3. MVC詳解 什么是真正的"框架"
責(zé)任編輯:yangsai 來(lái)源: ITPUB論壇
相關(guān)推薦

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數(shù)據(jù)驗(yàn)證

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-31 12:43:59

ASP.NET MVC

2009-07-24 13:20:44

MVC框架ASP.NET

2009-06-01 10:23:31

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

2009-09-10 09:50:47

ASP.NET MVC

2009-07-23 16:59:31

ASP.NET認(rèn)證Form表單

2009-11-24 15:11:21

ASP.NET MVC

2009-07-22 16:02:39

ASP.NET MVCPagedList

2009-07-29 09:59:10

ASP.NET For

2009-07-22 18:07:55

論壇應(yīng)用程序ASP.NET MVC

2009-07-22 09:11:02

Action方法ASP.NET MVC

2009-07-23 15:44:39

ASP.NET MVC

2009-07-22 10:09:59

ASP.NET MVC

2009-07-23 14:31:20

ASP.NET MVC
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)