温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ASP.NET MVC5验证之Fluent Validation的示例分析

发布时间:2021-09-01 11:48:47 来源:亿速云 阅读:92 作者:小新 栏目:开发技术

这篇文章将为大家详细讲解有关ASP.NET MVC5验证之Fluent Validation的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

Fluent Validation是一个开源的.NET类库,它使用Fluent接口和lambda表达式,来为实体做验证。Fluent Validation是专门为实体做验证使用的。它的优点是:把验证逻辑和你代码的业务逻辑分别开了。这就是AOP的思想。就是横切关注点。你只需要关注某一个模块。这样就保证了代码的纯洁度。

Fluent Validation开源地址: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.
面向方面程序设计作为一种新的软件开发范型,能够实现横切关注点的模块化,其特有的语言元素和功能为切片增加了难度。
好了,废话太多,直接进入正题,
首先我们新建一个空白的MVC项目:在Model文件夹下新建一个类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; }
 }
}

然后新建一个文件夹Validator,在里面添加一个类CustomerValidator 

ASP.NET MVC5验证之Fluent Validation的示例分析

既然是要使用Fluent Validation,那么就是要引用它的类库了。 

ASP.NET MVC5验证之Fluent Validation的示例分析

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:AbstractValidator<Customer>
 {
  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);
  }
 }
}

修改一下,默认的路由:

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

ASP.NET MVC5验证之Fluent Validation的示例分析

什么都不输入,直接点击Create:

ASP.NET MVC5验证之Fluent Validation的示例分析

输入Name,不输入Email

ASP.NET MVC5验证之Fluent Validation的示例分析

输入Name,Email输入非法的数据

ASP.NET MVC5验证之Fluent Validation的示例分析

输入合法的数据:

ASP.NET MVC5验证之Fluent Validation的示例分析

这里就完成了Fluent Validation验证。大家可以看到,这样的验证是不是干净简洁多了,配置信息都在一个类中,方便维护和扩展。不想数据注解那样,把验证信息和实体混合了。

关于“ASP.NET MVC5验证之Fluent Validation的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI