我正在尝试将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)
是密封的。有什么想法吗?
答案 0 :(得分: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);
}