首页 > 其他分享 >net 6使用FluentValidation校验请求数据

net 6使用FluentValidation校验请求数据

时间:2023-06-01 22:25:42浏览次数:41  
标签:Name FluentValidation 校验 class using net public

1,nuget增加 FluentValidation.AspNetCore
2.  DI中添加
builder.Services.AddFluentValidation(opt=>{
    opt.RegisterValidatorsFromAssembly( Assembly.GetEntryAssembly());
});
3. 添加具体的校验类
using FluentValidation;

namespace EShopWebApi.Controllers;

public class UserUpdateRequestValidator : AbstractValidator<UserUpdateRequest>
{
    public UserUpdateRequestValidator()
    {
        RuleFor(c=>c.Name).MaximumLength(2).WithErrorCode("403").WithMessage("Name长度不能超过2");
    }
}

其他代码:

using Microsoft.AspNetCore.Mvc;

[ApiController]
[Route("[controller]")]
public class TestController
{
    [HttpPost("Test")]
    public async Task<ActionResult<string>> Test([FromBody] UserUpdateRequest request)
    {
        return await Task.FromResult("sd");
    }
}
using System.ComponentModel.DataAnnotations;

public class UserUpdateRequest{
    // 这种校验与实体类耦合
    // [MaxLength(2)]
    public string Name { get; set; }
    public int Age { get; set; }
}

 

标签:Name,FluentValidation,校验,class,using,net,public
From: https://www.cnblogs.com/Insist-Y/p/17450391.html

相关文章

  • Fortinet 方案初了解
    Fortinet解决方案初了解目录Fortinet解决方案初了解企业数字化转型Forinet解决方案介绍使用场景公司介绍Fortinet产品和方案的核心永远是以安全为核心、以安全做为驱动,在一众厂商当中比较特别!企业数字化转型我们经常会在各种场景看到“企业数字化转型”,“互联网+”这......
  • Kubernetes CKA考试之Killer Simulator(下)
    目录Question16|NamespacesandApiResourcesQuestion17|FindContainerofPodandcheckinfoQuestion18|FixKubeletQuestion19|CreateSecretandmountintoPodQuestion20|UpdateKubernetesVersionandjoinclusterQuestion21|CreateaStaticPod......
  • .Net NPOI Excel 导出
    NPOI导出Excel最终效果图   环境:Revit,WPF,NPOI2.5.6,.NetFramework4.7.2一、引用NPOI 右击项目引用,选择"管理NuGet程序包",在浏览搜索NPOI,选择版本(我这里是2.5.6),安装   安装成功后,引用里会出现这四个引用(NPOI,NPOI.OOXML,NPOI.OpenXml......
  • nethunter终端进入KALI时报错问题
    在我的设备安装Nethunter成功后,使用NetHunter内置的连接终端工具连接时报了一个这样的错误zsh:failedtoloadmodule`zsh/zle':/usr/lib/aarch64-linux-gnu/zsh/5.9/zsh/zle.so:cannotopensharedobjectfile:Nosuchfileordirectorykali#使用SSH连接也是一样的错......
  • net.sf.json-lib 下载不下来
    必须加上classifier官网:http://json-lib.sourceforge.net/Json-libcomesintwoflavors,dependingonthejdkcompatibility.json-lib-x.x-jdk13iscompatiblewithJDK1.3.1andupwards.json-lib-x.x-jdk15iscompatiblewithJDK1.5,includessupportforEnums......
  • .net core Abp定时任务实现
     publicclassUseTimeJob:Job//重点是继承Job {publicreadonlyIUserCourseJobTimeService_userCourseJobTimeService;publicreadonlyIOrganizationAppService_organizationAppService;privatereadonlyIOfflineCourseLiveRecordServic......
  • 使用Minio Clinet将老版本Minio的数据迁移到新版本的Minio
    1.关于MinioClient:MinIOClient是一个命令行工具,用于与Minio或云存储服务进行交互。它支持文件系统和AmazonS3兼容的云存储服务(AWSSignaturev2和v4)。MinIOClientmc命令行工具提供了ls、cat、cp、mirror和diff等UNIX命令的现代替代方案,支持文件系统和Amazons3兼容的云......
  • 介绍.NET几种人脸识别组件
    人脸识别技术在现代社会中扮演着越来越重要的角色,比如人脸识别门禁、人脸识别支付、甚至人脸识别网站登录等。 最近有群友问.NET有没有人脸识别的组件,小编查阅相关资料介绍下面几种.NET人脸识别组件供大家参考。1、MicrosoftAzureFaceAPI简介:MicrosoftAzureFaceAPI是微软......
  • fortinet sql注入 语义分析检测
    Syntax-basedSQLInjectionDetectionUsingregularexpressionbasedsignaturestodetectSQLinjectionattacksiscoretoaWAFsolutionhoweveritdoesnotgowithoutissues.DuetothenatureoftheSQLlanguagebeingsimilartotheEnglishgrammarfalse......
  • Kubernetes(k8s)健康性检查:livenessprobe探测和readinessprobe探测
    目录一.系统环境二.前言三.Kubernetes健康性检查简介四.创建没有探测机制的pod五.添加livenessprobe探测5.1使用command的方式进行livenessprobe探测5.2使用httpGet的方式进行livenessprobe探测5.3使用tcpSocket的方式进行livenessprobe探测六.readinessprobe探测七.总结一.系......