前面兩篇文章學(xué)習(xí)到了,服務(wù)端驗(yàn)證,和客戶端的驗(yàn)證,但大家有沒(méi)有發(fā)現(xiàn),這兩種驗(yàn)證各自都有弊端,服務(wù)器端的驗(yàn)證,驗(yàn)證的邏輯和代碼的邏輯混合在一起了,如果代碼量很大的話,以后維護(hù)擴(kuò)展起來(lái),就不是很方便。而客戶端的驗(yàn)證,必須要啟用客戶端驗(yàn)證,也就是在配置文件中配置相應(yīng)的節(jié)點(diǎn),并且還要引入Jquery插件。如果人為的在瀏覽器上,禁用了js腳本,那么客戶端驗(yàn)證就不起作用了,所以在這里,我將繼續(xù)學(xué)習(xí)另外一個(gè)驗(yàn)證,也就是Fluent Validation。
Fluent Validation是一個(gè)開(kāi)源的.NET類庫(kù),它使用Fluent接口和lambda表達(dá)式,來(lái)為實(shí)體做驗(yàn)證。Fluent Validation是專門(mén)為實(shí)體做驗(yàn)證使用的。它的優(yōu)點(diǎn)是:把驗(yàn)證邏輯和你代碼的業(yè)務(wù)邏輯分別開(kāi)了。這就是AOP的思想。就是橫切關(guān)注點(diǎn)。你只需要關(guān)注某一個(gè)模塊。這樣就保證了代碼的純潔度。
Fluent Validation開(kāi)源地址:https://github.com/JeremySkinner/fluentvalidation
例句:
Aspect-oriented program is a new software development paradigm that enables modular implementation of cross-cutting concerns,and poses difficulties for slicing of aspect-oriented programs.
面向方面程序設(shè)計(jì)作為一種新的軟件開(kāi)發(fā)范型,能夠?qū)崿F(xiàn)橫切關(guān)注點(diǎn)的模塊化,其特有的語(yǔ)言元素和功能為切片增加了難度。
好了,廢話太多,直接進(jìn)入正題,
首先我們新建一個(gè)空白的MVC項(xiàng)目:在Model文件夾下新建一個(gè)類Customer:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Server_Side_Validation_IN_MVC.Models
{
public class Customer
{
public string Name { get; set; }
public string Email { get; set; }
}
}
然后新建一個(gè)文件夾Validator,在里面添加一個(gè)類CustomerValidator
既然是要使用Fluent Validation,那么就是要引用它的類庫(kù)了。
CustomerValidator類中,繼承AbstractValidator抽象類,(PS:這里和EF中的Fluent API類似,EF中是繼承EntityTypeConfiguration類)
using FluentValidation;
using Server_Side_Validation_IN_MVC.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Server_Side_Validation_IN_MVC.Validator
{
public class CustomerValidator:AbstractValidatorCustomer>
{
public CustomerValidator()
{
RuleFor(s => s.Name).NotEmpty().WithMessage("名字不能為空");
RuleFor(s => s.Email).NotEmpty().WithMessage("電子郵件不能為空");
RuleFor(s => s.Email).EmailAddress().WithMessage("電子郵件格式不合法");
}
}
}
控制器中的代碼:
using FluentValidation.Results;
using Server_Side_Validation_IN_MVC.Models;
using Server_Side_Validation_IN_MVC.Validator;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Server_Side_Validation_IN_MVC.Controllers
{
public class CustomerController : Controller
{
// GET: Customer
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(Customer model)
{
CustomerValidator validator = new CustomerValidator();
ValidationResult result = validator.Validate(model);
if (result.IsValid)
{
ViewBag.Name = model.Name;
ViewBag.Email = model.Email;
}
else
{
foreach (var item in result.Errors)
{
ModelState.AddModelError(item.PropertyName, item.ErrorMessage);
}
}
return View(model);
}
}
}
修改一下,默認(rèn)的路由:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Customer", action = "Index", id = UrlParameter.Optional }
);
}
什么都不輸入,直接點(diǎn)擊Create:
輸入Name,不輸入Email
輸入Name,Email輸入非法的數(shù)據(jù)
輸入合法的數(shù)據(jù):
這里就完成了Fluent Validation驗(yàn)證。大家可以看到,這樣的驗(yàn)證是不是干凈簡(jiǎn)潔多了,配置信息都在一個(gè)類中,方便維護(hù)和擴(kuò)展。不想數(shù)據(jù)注解那樣,把驗(yàn)證信息和實(shí)體混合了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:- asp.net 驗(yàn)證碼生成和刷新及驗(yàn)證
- Asp.net Mvc 身份驗(yàn)證、異常處理、權(quán)限驗(yàn)證(攔截器)實(shí)現(xiàn)代碼
- ASP.net 驗(yàn)證碼實(shí)現(xiàn)代碼(C#)
- asp.net(C#) 生成隨機(jī)驗(yàn)證碼的代碼
- ASP.NET表單驗(yàn)證方法詳解
- jQuery 驗(yàn)證插件 Web前端設(shè)計(jì)模式(asp.net)
- Asp.net(C#)實(shí)現(xiàn)驗(yàn)證碼功能代碼
- 注冊(cè)頁(yè)實(shí)現(xiàn)激活郵箱驗(yàn)證(asp.net c#)
- asp.net身份驗(yàn)證方式介紹
- asp.net 簡(jiǎn)單驗(yàn)證碼驗(yàn)證實(shí)現(xiàn)代碼