0

我正在尝试扩展CachedDataAnnotationsModelMetadataProvider到不缓存 Custom 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)

被密封。有任何想法吗 ?

4

1 回答 1

0

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

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);
}
于 2015-03-18T11:06:41.583 回答