[英]Asp.net Web Api - Validation Attribute getting invoked twice
我已經定義了一個從ValidationAttribute派生的新屬性。 例如
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class ValidateDataAttribute : ValidationAttribute
{
public override bool IsValid(object value)
{
// some logic
return true;
}
}
我面臨的問題是,對於給定的Post / Put請求,“IsValid”方法被調用兩次。 如果模型狀態無效,則會導致重復出現錯誤消息。 知道什么可能出錯嗎?
樣品用法:
public class Test
{
[Required]
[ValidateData]
public string Data {get;set;}
}
您已添加[必需]和[ValidateData]屬性。
我想這應該可以正常工作,如果您刪除所需的屬性,因為它做同樣的事情。 兩者都繼承自ValidateAttribute
public class Test { [Required] public string Data {get;set;} }
請查看所需的屬性類
// Summary:
// Specifies that a data field value is required.
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
public class RequiredAttribute : ValidationAttribute
{
// Summary:
// Initializes a new instance of the System.ComponentModel.DataAnnotations.RequiredAttribute
// class.
public RequiredAttribute();
// Summary:
// Gets or sets a value that indicates whether an empty string is allowed.
//
// Returns:
// true if an empty string is allowed; otherwise, false. The default value is
// false.
public bool AllowEmptyStrings { get; set; }
// Summary:
// Checks that the value of the required data field is not empty.
//
// Parameters:
// value:
// The data field value to validate.
//
// Returns:
// true if validation is successful; otherwise, false.
//
// Exceptions:
// System.ComponentModel.DataAnnotations.ValidationException:
// The data field value was null.
public override bool IsValid(object value);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.