扩展 CachedDataAnnotationsModelMetadataProvider 以不缓存一些 DataAnnotations

Extending CachedDataAnnotationsModelMetadataProvider to not cache some DataAnnotations

我正在尝试扩展 CachedDataAnnotationsModelMetadataProvider 以不缓存自定义 ValidationAttribute。 我怎样才能做到这一点?我试着查看 aspnetwebstack,但它太复杂了,无法得到答案;我需要覆盖什么,因为受保护的覆盖

protected override CachedDataAnnotationsModelMetadata CreateMetadataFromPrototype(
            CachedDataAnnotationsModelMetadata prototype,
            Func<object> modelAccessor)

protected override CachedDataAnnotationsModelMetadata CreateMetadataPrototype(
            IEnumerable<Attribute> attributes,
            Type containerType,
            Type modelType,
            string propertyName)

CachedAssociatedMetadataProvider<TModelMetadata>方法

protected sealed override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)

被封了。有什么想法吗?

我的问题是由于构造函数中属性的初始化,客户端验证没有更新。所以我将 属性 加载到

public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
    var userSettings = ServiceLocator.Resolve<UserSettings>();
    if (userSettings != null)
    {
        // this was the required field to update
        this.StringLengthAttribute.MinimumLength = userSettings.MinRequiredPasswordLength;
    }

    yield return new ModelClientValidationStringLengthRule(this.ErrorMessage, this.StringLengthAttribute.MinimumLength, this.StringLengthAttribute.MaximumLength);
}