zoukankan      html  css  js  c++  java
  • ASP.NET Core 3.x RESTful API学习记录--输入验证:IValidatableObject

    需要验证的Dto模型 继承于IValidatableObject

    public class ValidatableMovie : IValidatableObject
    {
        private const int _classicYear = 1960;
    
        public int Id { get; set; }
    
        [Required]
        [StringLength(100)]
        public string Title { get; set; }
    
        [DataType(DataType.Date)]
        [Display(Name = "Release Date")]
        public DateTime ReleaseDate { get; set; }
    
        [Required]
        [StringLength(1000)]
        public string Description { get; set; }
    
        [Range(0, 999.99)]
        public decimal Price { get; set; }
    
        public Genre Genre { get; set; }
    
        public bool Preorder { get; set; }
    
        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            if (Genre == Genre.Classic && ReleaseDate.Year > _classicYear)
            {
                yield return new ValidationResult(
                    $"Classic movies must have a release year no later than {_classicYear}.",
                    new[] { nameof(ReleaseDate) });
            }
        }
    }

    官方文档:https://docs.microsoft.com/zh-cn/aspnet/core/mvc/models/validation?view=aspnetcore-3.1#ivalidatableobject

  • 相关阅读:
    SpringBoot介绍
    linux运行jar以及vi
    linux文件命名
    数据库 mysql
    SSM框架-Spring
    SSM框架-mybatis
    SSM框架-SpringMVC
    设计模式-策略模式
    设计模式-单例模式
    Java多线程实现和JUC介绍
  • 原文地址:https://www.cnblogs.com/cqqinjie/p/13372399.html
Copyright © 2011-2022 走看看