這篇文章主要為大家展示了“ASP.NET全棧開發(fā)教程之前后臺(tái)校驗(yàn)結(jié)合的示例分析”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“ASP.NET全棧開發(fā)教程之前后臺(tái)校驗(yàn)結(jié)合的示例分析”這篇文章吧。
使用示例:
首先我們使用nuget安裝 FluentValidation
Nuget安裝命令:Install-Package FluentValidation -Version 7.6.104
由于FluentValidation的驗(yàn)證是基于模型的,所以,我們先來建立一個(gè)Person
public class Person { /// <summary> /// 姓名 /// </summary> public string Name { get; set; } /// <summary> /// 年齡 /// </summary> public int Age { get; set; } /// <summary> /// 性別 /// </summary> public bool Sex { get; set; } }
有了模型,要想驗(yàn)證模型,肯定得有驗(yàn)證器才行,于是我們?cè)趧?chuàng)建一個(gè)Person的驗(yàn)證器
public class PersonValidator : AbstractValidator<Person> { public PersonValidator() { this.RuleFor(p => p.Name) .NotEmpty() .WithMessage("名字不能為空"); this.RuleFor(p => p.Age) .NotEmpty() .WithMessage("年齡不能為空!!"); } }
驗(yàn)證器必須繼承自AbstractValidator<T>
,泛型T表示該驗(yàn)證器驗(yàn)證的實(shí)體,在構(gòu)造函數(shù)里通過this.RuleFor
為指定的屬性設(shè)置驗(yàn)證,在上述配置中為Person的Name和Age屬性都設(shè)置了非空,并且給出了相應(yīng)的錯(cuò)誤消息。
現(xiàn)在實(shí)體有了,驗(yàn)證器也有了,就差東風(fēng)了。
class Program { static void Main(string[] args) { Server(new Person { }); Console.ReadKey(true); } private static void Server(Person model) { var validator = new PersonValidator(); ValidationResult result = validator.Validate(model); if (!result.IsValid) { foreach (var failure in result.Errors) { //失敗的屬性名稱,如錯(cuò)誤信息 Console.WriteLine("Property " + failure.PropertyName + " failed validation. Error was: " + failure.ErrorMessage); } } Console.WriteLine("驗(yàn)證完成!!"); } }
在Server方法中需要一個(gè)參數(shù)Person,Person是個(gè)對(duì)象,里面有3個(gè)屬性Name、Age、Sex,在Server內(nèi)部我們創(chuàng)建了一個(gè)Person驗(yàn)證器對(duì)象,用它來驗(yàn)證我們的參數(shù)model, 驗(yàn)證后會(huì)返回一個(gè)驗(yàn)證結(jié)果。這個(gè)結(jié)果有2個(gè)重要的參數(shù),第一是IsValid,驗(yàn)證通過的時(shí)候返回True,第二個(gè)是Errors,他里面存放的是所有驗(yàn)證失敗的信息,在驗(yàn)證失敗的時(shí)候通過遍歷Errors即可獲取到所有錯(cuò)誤信息,如上所示通過錯(cuò)誤信息的PropertyName就能知道是哪個(gè)對(duì)象出錯(cuò)了,而ErrorMessage自然就對(duì)應(yīng)這個(gè)Property所出錯(cuò)內(nèi)容的WithMessage了。下面是運(yùn)行結(jié)果
至此,我們能夠使用基本驗(yàn)證了。
但在我們實(shí)際應(yīng)用中不一定會(huì)像上述案例一樣一帆風(fēng)順,也許我們的Person里會(huì)有一個(gè)Address屬性,他的類型不是一個(gè)String,而是一個(gè)對(duì)象Address,如下所示
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; } }
public class Address { /// <summary> /// 家庭地址 /// </summary> public string Home { get; set; } /// <summary> /// 家庭電話 /// </summary> public string Phone { get; set; } }
假如Address也有一個(gè)驗(yàn)證器
public class AddressValidator : AbstractValidator<Address> { public AddressValidator() { this.RuleFor(m => m.Home) .NotEmpty() .WithMessage("家庭住址不能為空"); this.RuleFor(m => m.Phone) .Length(11, 12) .WithMessage("電話必須是11-12位之間"); } }
現(xiàn)在需求變了,在Person實(shí)體的要求里不僅要求Name、Age不能為空,并且還要求Address下的Home和Phone滿足AddressValidator的要求,這可怎么辦呢?
當(dāng)然你可以像這樣做
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.Home) .MinimumLength(5) .WithMessage("家庭住址短長度為5"); } }
通過Person.Address去給每一個(gè)屬性配置校驗(yàn),這并不是不能完成,但如果Address屬性比較比較多,在配置起來的時(shí)候重復(fù)工作量大大提高,并且人家AddressValidator已經(jīng)完成配置了,你再來配置一遍,可以算是在浪費(fèi)生命嗎?
幸好,F(xiàn)luentValidation提供了一種為屬性設(shè)置驗(yàn)證器的機(jī)制。
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) .NotNull() .WithMessage("地址不能為空") .SetValidator(new AddressValidator()); } }
通過這樣我們就將Address下的Home和Phome的驗(yàn)證追加到Person的Address屬性上去了。
以上是“ASP.NET全棧開發(fā)教程之前后臺(tái)校驗(yàn)結(jié)合的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
名稱欄目:ASP.NET全棧開發(fā)教程之前后臺(tái)校驗(yàn)結(jié)合的示例分析-創(chuàng)新互聯(lián)
地址分享:http://chinadenli.net/article42/dsjdhc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、電子商務(wù)、網(wǎng)站營銷、網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈、用戶體驗(yàn)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容