ASP.NET全棧開發(fā)教程之在MVC中使用服務端驗證的方法
前言
上一章我們在控制臺中基本的了解了FluentValidation是如何簡潔,優(yōu)雅的完成了對實體的驗證工作,今天我們將在實戰(zhàn)項目中去應用它。
首先我們創(chuàng)建一個ASP.NET MVC項目,本人環(huán)境是VS2017,

創(chuàng)建成功后通過在Nuget中使用 Install-Package FluentValidation -Version 7.6.104 安裝FluentValidation
在Model文件夾中添加兩個實體Address 和 Person
public class Address
{
public string Home { get; set; }
public string Phone { get; set; }
}
public class Person
{
/// <summary>
/// 姓名
/// </summary>
public string Name { get; set; }
/// <summary>
/// 年齡
/// </summary>
public int Age { get; set; }
/// <summary>
/// 性別
/// </summary>
public bool Sex { get; set; }
/// <summary>
/// 地址
/// </summary>
public Address Address { get; set; }
}
緊接著創(chuàng)建實體的驗證器
public class AddressValidator : AbstractValidator<Address>
{
public AddressValidator()
{
this.RuleFor(m => m.Home)
.NotEmpty()
.WithMessage("家庭住址不能為空");
this.RuleFor(m => m.Phone)
.NotEmpty()
.WithMessage("手機號碼不能為空");
}
}
public class PersonValidator : AbstractValidator<Person>
{
public PersonValidator()
{
this.RuleFor(p => p.Name)
.NotEmpty()
.WithMessage("姓名不能為空");
this.RuleFor(p => p.Age)
.NotEmpty()
.WithMessage("年齡不能為空");
this.RuleFor(p => p.Address)
.SetValidator(new AddressValidator());
}
}
為了更好的管理驗證器,我建議將使用一個Manager者來管理所有驗證器的實例。如ValidatorHub
public class ValidatorHub
{
public AddressValidator AddressValidator { get; set; } = new AddressValidator();
public PersonValidator PersonValidator { get; set; } = new PersonValidator();
}
現(xiàn)在我們需要創(chuàng)建一個頁面,在默認的HomeController 控制器下添加2個Action:ValidatorTest,他們一個用于展示頁面,另一個則用于提交。
[HttpGet]
public ActionResult ValidatorTest()
{
return View();
}
[HttpPost]
public ActionResult ValidatorTest(Person model)
{
return View();
}
為 ValidatorTest 添加視圖,選擇Create模板,實體為Person

將默認的@Html全部刪掉,因為在我們本次介紹中不需要,我們的目標是搭建一個前后端分離的項目,而不要過多的依賴于MVC。
最終我們將表單改寫成了
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Person</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
<label for="Name" class="control-label col-md-2">姓名</label>
<div class="col-md-10">
<input type="text" name="Name" class="form-control" />
</div>
</div>
<div class="form-group">
<label for="Age" class="control-label col-md-2">年齡</label>
<div class="col-md-10">
<input type="text" name="Age" class="form-control" />
</div>
</div>
<div class="form-group">
<label for="Home" class="control-label col-md-2">住址</label>
<div class="col-md-10">
<input type="text" name="Address.Home" class="form-control" />
</div>
</div>
<div class="form-group">
<label for="Phone" class="control-label col-md-2">電話</label>
<div class="col-md-10">
<input type="text" name="Address.Phone" class="form-control" />
</div>
</div>
<div class="form-group">
<label for="Sex" class="control-label col-md-2">性別</label>
<div class="col-md-10">
<div class="checkbox">
<input type="checkbox" name="Sex" />
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
注意,由于我們的實體Person中存在復雜類型Address,我們都知道,表單提交默認是Key:Value形式,而在傳統(tǒng)表單的key:value中,我們無法實現(xiàn)讓key為Address的情況下Value為一個復雜對象,因為input一次只能承載一個值,且必須是字符串。實際上MVC中存在模型綁定,此處不作過多介紹(因為我也忘記了-_-||)。
簡單的說就是他能根據(jù)你所需要類型幫我們自動盡可能的轉(zhuǎn)換,我們目前只要知道如何正確使用,在Address中存在Home屬性和Phone屬性,我們可以將表單的name設置為Address.Home,MVC的模型綁定會將Address.Home解析到對象Address上的Home屬性去。
簡單的校驗方式我也不過多介紹了。再上一章我們已經(jīng)了解到通過創(chuàng)建一個實體的驗證器來對實體進行驗證,然后通過IsValid屬性判斷是否驗證成功。對,沒錯,對于大家來說這太簡單了。但我們每次校驗都創(chuàng)建一個驗證器是否顯得有點麻煩呢?不要忘了我們剛剛創(chuàng)建了一個ValidatorHub,我們知道控制器默認繼承自Controller,如果我們想為控制器擴展一些能力呢?現(xiàn)在我要創(chuàng)建一個ControllerEx了,并繼承自Controller。
public class ControllerEx : Controller
{
protected Dictionary<string, string> DicError { get; set; } = new Dictionary<string, string>();
protected ValidatorHub ValidatorHub { get; set; } = new ValidatorHub();
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
ViewData["Error"] = DicError;
}
protected void ValidatorErrorHandler(ValidationResult result)
{
foreach (var failure in result.Errors)
{
if (!this.DicError.ContainsKey(failure.PropertyName))
{
this.DicError.Add(failure.PropertyName, failure.ErrorMessage);
}
}
}
}
在ControllerEx里我創(chuàng)建了一個ValidatorHub屬性,正如其名,他內(nèi)部存放著各種驗證器實體呢。有了它,我們可以在需要驗證的Action中通過this.ValidatorHub.具體驗證器就能完成具體驗證工作了,而不需要再去每次new 一個驗證器。
同樣我定義了一個DicError的鍵值對集合,他的鍵和值類型都是string。key是驗證失敗的屬性名,而value則是驗證失敗后的錯誤消息,它是用來存在驗證的結(jié)果的。
在這里我還定義了一個ValidatorErrorHandler的方法,他有一個參數(shù)是驗證結(jié)果,通過名稱我們大致已經(jīng)猜到功能了,驗證錯誤的處理,對驗證結(jié)果的錯誤信息進行遍歷,并將錯誤信息添加至DicError集合。
最終我需要將這個DicError傳遞給View,簡單的辦法是ViewData["Error"] 但我不想在每個頁面都去這么干,因為這使我要重復多次寫這行代碼,我會厭倦它的。很棒的是MVC框架為我們提供了Filter(有的地方也稱函數(shù)鉤子,切面編程,過濾器),能夠方便我們在生命周期的不同階段進行控制,很顯然,我的需求是在每次執(zhí)行完Action后要在末尾添加一句ViewData["Error"]=DicError。于是我重寫了OnActionExecuted方法,僅添加了 ViewData["Error"] = DicError;
現(xiàn)在我只需要將HomeController繼承自ControllerEx即可享受以上所有功能了。
現(xiàn)在基本工作基本都完成了,但我們還忽略了一個問題,我錯誤是存在了ViewData["Error"]里傳遞給View,只不過難道我們在驗證錯誤的時候在頁面顯示一個錯誤列表?像li一樣?這顯然不是我們想要的。我們還需要一個幫助我們合理的顯示錯誤信息的函數(shù)。在Razor里我們可以對HtmlHelper進行擴展。于是我為HtmlHelper擴展了一個方法ValidatorMessageFor
public static class ValidatorHelper
{
public static MvcHtmlString ValidatorMessageFor(this HtmlHelper htmlHelper, string property, object error)
{
var dicError = error as Dictionary<string, string>;
if (dicError == null) //沒有錯誤
{
// 不會等于空
}
else
{
if (dicError.ContainsKey(property))
{
return new MvcHtmlString(string.Format("<p>{0}</p>", dicError[property]));
}
}
return new MvcHtmlString("");
}
}
在ValidatorMessaegFor里需要2個參數(shù)property 和 error
前者是需要顯示的錯誤屬性名,后者則是錯誤對象即ViewData["Error"],功能很簡單,在發(fā)現(xiàn)錯誤對象里存在key為錯誤屬性名的時候?qū)alue用一個p標簽包裹起來返回,value即為錯誤屬性所對應的錯誤提示消息。
現(xiàn)在我們還需要在View每一個input下添加一句如: @Html.ValidatorMessageFor("Name", ViewData["Error"])即可。
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Person</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
<label for="Name" class="control-label col-md-2">姓名</label>
<div class="col-md-10">
<input type="text" name="Name" class="form-control" />
@Html.ValidatorMessageFor("Name", ViewData["Error"])
</div>
</div>
<div class="form-group">
<label for="Age" class="control-label col-md-2">年齡</label>
<div class="col-md-10">
<input type="text" name="Age" class="form-control" />
@Html.ValidatorMessageFor("Name", ViewData["Error"])
</div>
</div>
<div class="form-group">
<label for="Home" class="control-label col-md-2">住址</label>
<div class="col-md-10">
<input type="text" name="Address.Home" class="form-control" />
@Html.ValidatorMessageFor("Address.Home", ViewData["Error"])
</div>
</div>
<div class="form-group">
<label for="Phone" class="control-label col-md-2">電話</label>
<div class="col-md-10">
<input type="text" name="Address.Phone" class="form-control" />
@Html.ValidatorMessageFor("Address.Phone", ViewData["Error"])
</div>
</div>
<div class="form-group">
<label for="Sex" class="control-label col-md-2">性別</label>
<div class="col-md-10">
<div class="checkbox">
<input type="checkbox" name="Sex" />
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
到此我們的所有基本工作都已完成
[HttpPost]
public ActionResult ValidatorTest(Person model)
{
var result = this.ValidatorHub.PersonValidator.Validate(model);
if (result.IsValid)
{
return Redirect("https://www.baidu.com");
}
else
{
this.ValidatorErrorHandler(result);
}
return View();
}
通過我們在ControllerEx種的ValidatorHub來對實體Person進行校驗,如果校驗成功了....這里沒啥可干的就當跳轉(zhuǎn)一下表示咯,否則的話調(diào)用Ex中的ValidatorErrorHandler 將錯誤消息綁定到ViewData["Error"]中去,這樣就能在前端View渲染的時候?qū)㈠e誤消息顯示出來了。
接下來我們將程序跑起來。




正如大家所看到的,當我點擊提交的時候 雖然只有電話沒輸入但其他三個表單被清空了,也許我們會覺得不爽,當然如果你需要那相信你在看完上述的錯誤信息綁定后一定也能解決這個問題的,但事實上,我們并不需要它,\(^o^)/~
為什么呢?因為我們還要前端驗證啊,當前端驗證沒通過的時候根本無法發(fā)送到后端來,所以不用擔心用戶在一部分驗證失敗時已填寫的表單數(shù)據(jù)被清空掉。
這里提到在表單提交時需要前端校驗,既然有前端校驗了為何還要我們做后臺校驗呢?不是脫了褲子放屁嗎?事實上,前端校驗的作用在于優(yōu)化用戶體驗,減輕服務器壓力,也可以防住君子,但絕不能防止小人,由于Web客戶端的不確定性,任何東西都可以模擬的。如果不做服務端驗證,假如你的系統(tǒng)涉及金錢,也許那天你醒來就發(fā)現(xiàn)自己破產(chǎn)了。
來一個通過驗證的。


總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關文章
ASP.NET Core 3框架揭秘之 異步線程無法使用IServiceProvider問題
這篇文章主要介紹了ASP.NET Core 3框架揭秘之異步線程無法使用IServiceProvider問題,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-12-12
asp.net下用Aspose.Words for .NET動態(tài)生成word文檔中的圖片或水印的方法
本文詳細講解如何使用Aspose.Words for .NET的組件來生成word文檔與水印的方法,請看本文內(nèi)容。2010-04-04
記錄游客頁面訪問IP的簡易實現(xiàn)代碼 (asp.net+txt)
記錄游客頁面訪問IP的簡易實現(xiàn) (asp.net for notepad)2010-01-01
ASP.NET(C#)中操作SQLite數(shù)據(jù)庫實例
最近項目中有使用到SQLite數(shù)據(jù)庫,于是查找資料,編寫了一個ASP.NET基于C#語言的SQLite數(shù)據(jù)庫操作實例.大家看代碼就可以看懂了,和以往使用ADO.NET操作SQL數(shù)據(jù)庫類似.2009-12-12
Asp.net ajax實現(xiàn)任務提示頁面的簡單代碼
這篇文章介紹了Asp.net ajax實現(xiàn)任務提示頁面的簡單代碼,有需要的朋友可以參考一下2013-11-11

