.NET核心-自定义验证属性消息

.NET核心-自定义验证属性消息,.net,validation,attributes,core,.net,Validation,Attributes,Core,我正在尝试在.NETCore中执行自定义验证属性消息。其目的是使工作自动化,因此我不使用ErrorMessage参数。 我已完成所需的操作,如下所示: public class RequiredShortAttribute : RequiredAttribute, IClientModelValidator { public RequiredShortAttribute() : base() { ErrorMessageResourceType = typeof(

我正在尝试在.NETCore中执行自定义验证属性消息。其目的是使工作自动化,因此我不使用ErrorMessage参数。 我已完成所需的操作,如下所示:

public class RequiredShortAttribute : RequiredAttribute, IClientModelValidator
{
    public RequiredShortAttribute() : base()
    {
        ErrorMessageResourceType = typeof(ErrorResource);
        ErrorMessageResourceName = "RequiredShort";
    }

    public void AddValidation(ClientModelValidationContext context)
    {
        MergeAttribute(context.Attributes, "data-val-required", FormatErrorMessage(""));
    }

    bool MergeAttribute(IDictionary<string, string> attributes, string key, string value)
    {
        if (attributes.ContainsKey(key))
        {
            return false;
        }
        attributes.Add(key, value);
        return true;
    }
} 
公共类RequiredShortAttribute:RequiredAttribute,IClientModelValidator
{
public RequiredShortAttribute():base()
{
ErrorMessageResourceType=typeof(ErrorResource);
ErrorMessageResourceName=“RequiredShort”;
}
public void AddValidation(ClientModelValidationContext上下文)
{
MergeAttribute(context.Attributes,“需要数据值”,FormatErrorMessage(“”);
}
布尔合并属性(IDictionary属性、字符串键、字符串值)
{
if(attributes.ContainsKey(键))
{
返回false;
}
属性。添加(键、值);
返回true;
}
} 
但是,当我尝试以这种方式创建其他自定义属性时(如下面的StringLength),验证不起作用。我应该补充点什么吗

public class StringLengthShortAttribute : StringLengthAttribute, IClientModelValidator
{
    public StringLengthShortAttribute(int max) : base(max)
    {
        ErrorMessageResourceType = typeof(ErrorResource);
        ErrorMessageResourceName = "StringLengthShort";
    }

    public void AddValidation(ClientModelValidationContext context)
    {
        MergeAttribute(context.Attributes, "data-val-length", FormatErrorMessage(""));
    }

    bool MergeAttribute(IDictionary<string, string> attributes, string key, string value)
    {
        if (attributes.ContainsKey(key))
        {
            return false;
        }
        attributes.Add(key, value);
        return true;
    }
}
公共类StringLengthShortAttribute:StringLengthAttribute,IClientModelValidator
{
公共StringLengthShortAttribute(int max):基本(max)
{
ErrorMessageResourceType=typeof(ErrorResource);
ErrorMessageResourceName=“StringLengthShort”;
}
public void AddValidation(ClientModelValidationContext上下文)
{
MergeAttribute(context.Attributes,“数据值长度”,FormatErrorMessage(“”);
}
布尔合并属性(IDictionary属性、字符串键、字符串值)
{
if(attributes.ContainsKey(键))
{
返回false;
}
属性。添加(键、值);
返回true;
}
}

为什么不将验证器编写为:

    public class ShortStringLengthShortAttribute : StringLengthAttribute
    {
        public ShortStringLengthShortAttribute(int maximumLength) : base(maximumLength)
        {
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            ValidationResult validationResult = new StringLengthAttribute(MaximumLength).GetValidationResult(value, validationContext);
            if (validationResult == ValidationResult.Success)
            {
                return ValidationResult.Success;
            }

            return new ValidationResult("Put here the error message you want"); 
        }
    }