ASP.NET CORE 入门教程(附源码) (2)

数据验证特性ValidationAttribute

public abstract class ValidationAttribute : Attribute { /// <summary>Initializes a new instance of the <see cref="T:System.ComponentModel.DataAnnotations.ValidationAttribute"></see> class.</summary> protected ValidationAttribute(); /// <summary>Initializes a new instance of the <see cref="T:System.ComponentModel.DataAnnotations.ValidationAttribute"></see> class by using the function that enables access to validation resources.</summary> /// <param>The function that enables access to validation resources.</param> /// <exception cref="T:System.ArgumentNullException"><paramref>errorMessageAccessor</paramref> is null.</exception> protected ValidationAttribute(Func<string> errorMessageAccessor); /// <summary>Initializes a new instance of the <see cref="T:System.ComponentModel.DataAnnotations.ValidationAttribute"></see> class by using the error message to associate with a validation control.</summary> /// <param>The error message to associate with a validation control.</param> protected ValidationAttribute(string errorMessage); /// <summary>Gets or sets an error message to associate with a validation control if validation fails.</summary> /// <returns>The error message that is associated with the validation control.</returns> public string ErrorMessage { get; set; } /// <summary>Gets or sets the error message resource name to use in order to look up the <see cref="P:System.ComponentModel.DataAnnotations.ValidationAttribute.ErrorMessageResourceType"></see> property value if validation fails.</summary> /// <returns>The error message resource that is associated with a validation control.</returns> public string ErrorMessageResourceName { get; set; } /// <summary>Gets or sets the resource type to use for error-message lookup if validation fails.</summary> /// <returns>The type of error message that is associated with a validation control.</returns> public Type ErrorMessageResourceType { get; set; } /// <summary>Gets the localized validation error message.</summary> /// <returns>The localized validation error message.</returns> protected string ErrorMessageString { get; } /// <summary>Gets a value that indicates whether the attribute requires validation context.</summary> /// <returns>true if the attribute requires validation context; otherwise, false.</returns> public virtual bool RequiresValidationContext { get; } /// <summary>Applies formatting to an error message, based on the data field where the error occurred.</summary> /// <param>The name to include in the formatted message.</param> /// <returns>An instance of the formatted error message.</returns> public virtual string FormatErrorMessage(string name); /// <summary>Checks whether the specified value is valid with respect to the current validation attribute.</summary> /// <param>The value to validate.</param> /// <param>The context information about the validation operation.</param> /// <returns>An instance of the <see cref="System.ComponentModel.DataAnnotations.ValidationResult"></see> class.</returns> public ValidationResult GetValidationResult( object value, ValidationContext validationContext); /// <summary>Determines whether the specified value of the object is valid.</summary> /// <param>The value of the object to validate.</param> /// <returns>true if the specified value is valid; otherwise, false.</returns> public virtual bool IsValid(object value); /// <summary>Validates the specified value with respect to the current validation attribute.</summary> /// <param>The value to validate.</param> /// <param>The context information about the validation operation.</param> /// <returns>An instance of the <see cref="System.ComponentModel.DataAnnotations.ValidationResult"></see> class.</returns> protected virtual ValidationResult IsValid( object value, ValidationContext validationContext); /// <summary>Validates the specified object.</summary> /// <param>The object to validate.</param> /// <param>The <see cref="T:System.ComponentModel.DataAnnotations.ValidationContext"></see> object that describes the context where the validation checks are performed. This parameter cannot be null.</param> /// <exception cref="T:System.ComponentModel.DataAnnotations.ValidationException">Validation failed.</exception> public void Validate(object value, ValidationContext validationContext); /// <summary>Validates the specified object.</summary> /// <param>The value of the object to validate.</param> /// <param>The name to include in the error message.</param> /// <exception cref="T:System.ComponentModel.DataAnnotations.ValidationException"><paramref>value</paramref> is not valid.</exception> public void Validate(object value, string name); }

常用数据验证

RequiredAttribute

RegularExpressionAttribute

CompareAttribute

RangeAttribute

MaxAttribute

MinAttribute

StringLengthAttribute

DataTypeAttribute

服务器端使用

使用包含验证规则的类接收数据

使用ModelState.IsValid判断是否符合要求

前端使用

定义强类型视图并传递包含验证规则的业务数据模型

使用HtmlHelper.ValidationFor初始前端验证规则

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpyzys.html