나는이 오래지만,에 알고 메타 데이터에 지역화 된 메시지를 가져 오려면 DataAnnotationsModelValidator를 하위 클래스로 만들고 GetClientValidationRules 및 Validate를 재정 의하여 고유 한 메시지를 제공해야합니다.
DataAnnotationsModelValidatorProvider.RegisterAdapterFactory를 사용하여 어댑터를 등록하십시오.
팩터 리 위임을 생성하는 래퍼 팩토리 빌더를 구축했습니다. 여기서 out 매개 변수는 리플렉션을 통해 어셈블리의 모든 어댑터를 발견 할 때 루프 내부에서 사용하므로 RegisterAdpaterFactory를 호출하려면 각 어댑터의 특성 유형을 가져와야합니다. 나는 등록을 인라인 수도 있지만 어댑터를 사용하여이 후 다른 물건을 할/정보를
public static class ModelValidationFactory
{
/// <summary>
/// Builds a Lamda expression with the Func<ModelMetadata, ControllerContext, ValidationAttribute, ModelValidator> signature
/// to instantiate a strongly typed constructor. This used by the <see cref="DataAnnotationsModelValidatorProvider.RegisterAdapterFactory"/>
/// and used (ultimately) by <see cref="ModelValidatorProviderCollection.GetValidators"/>
/// </summary>
/// <param name="adapterType">Adapter type, expecting subclass of <see cref="ValidatorResourceAdapterBase{TAttribute}"/> where T is one of the <see cref="ValidationAttribute"/> attributes</param>
/// <param name="attrType">The <see cref="ValidationAttribute"/> generic argument for the adapter</param>
/// <returns>The constructor invoker for the adapter. <see cref="DataAnnotationsModelValidationFactory"/></returns>
public static DataAnnotationsModelValidationFactory BuildFactory(Type adapterType, out Type attrType)
{
attrType = adapterType.BaseType.GetGenericArguments()[0];
ConstructorInfo ctor = adapterType.GetConstructor(new[] { typeof(ModelMetadata), typeof(ControllerContext), attrType });
ParameterInfo[] paramsInfo = ctor.GetParameters();
ParameterExpression modelMetadataParam = Expression.Parameter(typeof(ModelMetadata), "metadata");
ParameterExpression contextParam = Expression.Parameter(typeof(ControllerContext), "context");
ParameterExpression attributeParam = Expression.Parameter(typeof(ValidationAttribute), "attribute");
Expression[] ctorCallArgs = new Expression[]
{
modelMetadataParam,
contextParam,
Expression.TypeAs(attributeParam, attrType)
};
NewExpression ctorInvoker = Expression.New(ctor, ctorCallArgs);
// (ModelMetadata metadata, ControllerContext context, ValidationAttribute attribute) => new {AdapterType}(metadata, context, ({AttrType})attribute)
return Expression
.Lambda(typeof(DataAnnotationsModelValidationFactory), ctorInvoker, modelMetadataParam, contextParam, attributeParam)
.Compile()
as DataAnnotationsModelValidationFactory;
}
}
이것은 또한 MVC3에서 작동하고, 나뿐만 아니라 MVC2 생각 때문이다.
이 말은 MVC2/3에서 작동하며 v4 미리보기에서 깨졌습니다? – RickAndMSFT