asp.net core FluentValidation的数据效验 的基本使用

FluentValidation 可以 提供 数据效验的 解耦性,通俗讲 就是 一个类 可以 有多种效验方法,相互都是独立文件,传输和效验 完全分开。并且 自定义 效验 比较 容易。

下面讲解 使用方式

1. 首先 建立 ModifyUser2.cs文件 用于模型类

using System.ComponentModel.DataAnnotations;

namespace aspnetcore014
{
    public class ModifyUser2
    {
        public string Name { get; set; }
        public string Email { get; set; }
        public string Password1 { get; set; }
        public string Password2 { get; set; }
    }
}

2.建立 ModifyUserIntendedEffect.cs文件 用于 效验。 这个自定义的类 必须继承 AbstractValidator<实体类> 并在 在 构造函数中 设置效验方法

using FluentValidation;
using FluentValidation.Validators;

namespace aspnetcore014
{
    public class ModifyUserIntendedEffect:AbstractValidator<ModifyUser2>
    {
        public ModifyUserIntendedEffect()
        {   //Must为自定义 效验
            RuleFor(x=>x.Name).NotNull().Length(3,10)
                .WithMessage("用户名必须3到10位");
            RuleFor(x => x.Email).NotNull().EmailAddress()
                .Must(x => x.EndsWith("@163.com") || x.EndsWith("@qq.com"))
                .WithMessage("必须是163或qq邮箱");
            RuleFor(x => x.Password1).NotNull().Length(5, 10)
                .WithMessage("密码长度为5到10")
                .Equal(x => x.Password2).WithMessage("两次密码必须相等");
        }
    }
}

3.测试程序

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace aspnetcore014.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class Test1Controller : ControllerBase
    {
        [HttpPut]
        public string ModifyUser2(ModifyUser2 modifyUser)
        {
            return "ok";
        }
    }
}

故意写错 返回结果为:

{
  "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
  "title": "One or more validation errors occurred.",
  "status": 400,
  "traceId": "00-9276e344142d80a35dcfbed128b34b09-3bd8a3713637dd3f-00",
  "errors": {
    "Name": [
      "用户名必须3到10位"
    ],
    "Email": [
      "'Email' 不是有效的电子邮件地址。",
      "必须是163或qq邮箱"
    ],
    "Password1": [
      "密码长度为5到10",
      "两次密码必须相等"
    ]
  }
}

引用nuget 包为:

FluentValidation.AspNetCore

在Program.cs 文件中配置 如下:

//启动自动验证
builder.Services.AddFluentValidationAutoValidation();
//注入 ModifyUser2 ModifyUserIntendedEffect 对应上面两个cs文件
builder.Services.AddScoped<IValidator<ModifyUser2>, ModifyUserIntendedEffect>();

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ASP.NET Core Web 项目中使用 Vue.js,您可以按照以下步骤进行操作: 1. 首先,确保您的开发环境中已安装 Node.js。您可以从 Node.js 官方网站(https://nodejs.org)下载并安装适合您操作系统的版本。 2. 创建 ASP.NET Core Web 项目:使用您选择的方法创建一个新的 ASP.NET Core Web 项目或打开现有项目。 3. 在项目根目录中初始化 Vue CLI:打开命令行界面,导航到您的项目根目录,并运行以下命令来初始化 Vue CLI: ``` vue create clientApp ``` 此命令将在项目根目录下创建一个名为 `clientApp` 的文件夹,并初始化 Vue CLI 项目。 4. 配置 Vue CLI:运行上述命令后,Vue CLI 会引导您进行一些配置选项的选择。您可以根据需求选择不同的配置,例如选择需要的特性和插件。 5. 开发 Vue 组件:在 `clientApp` 文件夹中,您可以开始编写 Vue 组件。您可以使用单文件组件 (SFC) 的方式编写组件,或者使用其他适合您的方式。 6. 集成 Vue.js 到 ASP.NET Core Web 项目:在 ASP.NET Core Web 项目中的 Razor 页面或视图中,引入 Vue.js 和您编写的 Vue 组件。您可以使用 `<script>` 标签引入 Vue.js 运行时或开发版本,使用 `<link>` 或 `<style>` 标签引入样式文件。 7. 配置 ASP.NET Core Web 项目以处理 Vue CLI 生成的文件:在 ASP.NET Core Web 项目中的 `Startup.cs` 文件中,确保在 `Configure` 方法中添加以下配置: ```csharp app.UseStaticFiles(new StaticFileOptions { RequestPath = "/clientApp", FileProvider = new PhysicalFileProvider( Path.Combine(Directory.GetCurrentDirectory(), "clientApp/dist") ) }); ``` 这将配置 ASP.NET Core Web 项目以提供 Vue CLI 生成的文件。 8. 运行项目:运行 ASP.NET Core Web 项目,并访问包含 Vue 组件的页面。您应该能够看到 Vue 组件在浏览器中正常运行。 通过上述步骤,您就可以在 ASP.NET Core Web 项目中使用 Vue.js,并开发和管理您的 Vue 组件。这样做可以让您更方便地利用 Vue.js 的功能和特性,与 ASP.NET Core Web 项目进行集成。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值