基本流程:
1.route增加一个 RouteHandler,设置区域文化。
2.扩展 DataAnnotationsModelMetadataProvider 适配器(Model的一些特征进行描述)
return View() 时 会触发 ModelMetadataProvider
参考:https://www.cnblogs.com/ywsoftware/p/5577000.html
3.扩展 DataAnnotationsModelValidatorProvider 适配器
4.注册 适配器。Global.cs
ModelMetadataProviders.Current = new MyModelMetadataProvider();//数据显示 适配器 ModelValidatorProviders.Providers.Clear();//清除验证适配器
ModelValidatorProviders.Providers.Add(new CustomValidationAttributeAdapterProvider());//自定义数据验证 适配器 自动会去找 Views/XXX/Lang/Language.resx,验证属性 Required 的错误新资源文件 CommonLanguage.Language
/// <summary>
/// 数据显示-适配器
/// </summary>
public class MyModelMetadataProvider : DataAnnotationsModelMetadataProvider
{
protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)
{
ModelMetadata metadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
if (containerType != null)
{
var TypeName = containerType.FullName.Replace("Models", "Views") + "s.Lang.Language";
var _type = CacheHelper.GetCache(TypeName) as Type;
if (_type == null) {
_type = System.Reflection.Assembly.GetAssembly(containerType).GetType(TypeName);
CacheHelper.SetCache(TypeName, _type);
}
if (_type != null)
{
List<PropertyInfo> ArrProperty = CacheHelper.GetCache(TypeName + "_Property") as List<PropertyInfo>;
if (ArrProperty == null)
{
ArrProperty = _type.GetProperties().ToList();
CacheHelper.SetCache(TypeName + "_Property", ArrProperty);
}
var Property = ArrProperty.Where(x => x.Name == propertyName).FirstOrDefault();
if (Property != null)
metadata.DisplayName = Property.GetValue(null, null).ToString();
}
}
return metadata;
}
}
/// <summary>
/// 数据验证适配器
/// </summary>
public class CustomValidationAttributeAdapterProvider : DataAnnotationsModelValidatorProvider
{
public CustomValidationAttributeAdapterProvider()
{
}
protected override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable<Attribute> attributes)
{
var validators = attributes.OfType<ValidationAttribute>();
var DisplayAttr = attributes.OfType<DisplayAttribute>().FirstOrDefault();
//记录验证属性(去除验证属性)
var allAttributes = attributes.Except(validators).ToList();
已在MyModelMetadataProvider : DataAnnotationsModelMetadataProvider 中扩展
//if (DisplayAttr != null)
//{
// if (DisplayAttr.ResourceType == null)
// {
// var _type = System.Reflection.Assembly.GetAssembly(metadata.ContainerType).GetType(metadata.ContainerType.FullName.Replace("Models", "Views") + "s.Lang.Language");
// DisplayAttr.ResourceType = _type;
// DisplayAttr.ShortName = _type.GetProperty(metadata.PropertyName).GetValue(null, null).ToString();
// }
//}
//必填属性
if (metadata.IsRequired)
{
var RequiredAttr = new RequiredAttribute();
RequiredAttr.ErrorMessageResourceType = typeof(CommonLanguage.Language);
RequiredAttr.ErrorMessageResourceName = "RequiredAttribute_ValidationError";
allAttributes.Add(RequiredAttr);
}
//枚举
if (metadata.ModelType.IsEnum)
{
var EnumAttr = new EnumDataTypeAttribute(metadata.ModelType);
EnumAttr.ErrorMessageResourceType = typeof(CommonLanguage.Language);
EnumAttr.ErrorMessageResourceName = "EnumDataTypeAttribute_TypeNeedsToBeAnEnum";
allAttributes.Add(EnumAttr);
}
foreach (ValidationAttribute validator in validators)
{
if (validator is RequiredAttribute)
{
continue;//跳过
}
if (validator is EnumDataTypeAttribute)
{
continue;//跳过
//if (validator.ErrorMessageResourceType != typeof(CommonLanguage.Language))
//{
// validator.ErrorMessageResourceType = typeof(CommonLanguage.Language);
// validator.ErrorMessageResourceName = "EnumDataTypeAttribute_TypeNeedsToBeAnEnum";
//}
//allAttributes.Add(validator);
//continue;
}
if (validator is MinLengthAttribute)
{
if (validator.ErrorMessageResourceType != typeof(CommonLanguage.Language))
{
validator.ErrorMessageResourceType = typeof(CommonLanguage.Language);
//StringLengthAttribute_ValidationError
validator.ErrorMessageResourceName = "MinLengthAttribute_ValidationError";
}
allAttributes.Add(validator);
continue;
}
if (validator is MaxLengthAttribute)
{
if (validator.ErrorMessageResourceType != typeof(CommonLanguage.Language))
{
validator.ErrorMessageResourceType = typeof(CommonLanguage.Language);
//StringLengthAttribute_ValidationError
validator.ErrorMessageResourceName = "MaxLengthAttribute_ValidationError";
}
allAttributes.Add(validator);
continue;
}
if (validator is RangeAttribute)
{
if (validator.ErrorMessageResourceType != typeof(CommonLanguage.Language))
{
validator.ErrorMessageResourceType = typeof(CommonLanguage.Language);
validator.ErrorMessageResourceName = "RangeAttribute_ValidationError";
//if (metadata.ModelType == typeof(int) || metadata.ModelType == typeof(long))
//else
// validator.ErrorMessageResourceName = "ValidationDefault_FloatRange";
}
allAttributes.Add(validator);
continue;
}
if (validator is StringLengthAttribute)
{
if (validator.ErrorMessageResourceType != typeof(CommonLanguage.Language))
{
StringLengthAttribute StrLenAttr = validator as StringLengthAttribute;
validator.ErrorMessageResourceType = typeof(CommonLanguage.Language);
if (StrLenAttr.MinimumLength > 0)
validator.ErrorMessageResourceName = "StringLengthAttribute_ValidationErrorIncludingMinimum";
else
validator.ErrorMessageResourceName = "StringLengthAttribute_ValidationError";
}
allAttributes.Add(validator);
continue;
}
if (validator is EmailAddressAttribute)
{
if (validator.ErrorMessageResourceType != typeof(CommonLanguage.Language))
{
validator.ErrorMessageResourceType = typeof(CommonLanguage.Language);
validator.ErrorMessageResourceName = "EmailAddressAttribute_Invalid";
}
allAttributes.Add(validator);
continue;
}
if (validator is RegularExpressionAttribute)
{
if (validator.ErrorMessageResourceType != typeof(CommonLanguage.Language))
{
validator.ErrorMessageResourceType = typeof(CommonLanguage.Language);
validator.ErrorMessageResourceName = "RegexAttribute_ValidationError";
}
allAttributes.Add(validator);
continue;
}
if (validator is System.ComponentModel.DataAnnotations.CompareAttribute)
{
if (validator.ErrorMessageResourceType != typeof(CommonLanguage.Language))
{
//var CompareAttr = validator as CompareAttribute;
validator.ErrorMessageResourceType = typeof(CommonLanguage.Language);
validator.ErrorMessageResourceName = "CompareAttribute_MustMatch";
}
allAttributes.Add(validator);
continue;
}
if (validator is System.Web.Mvc.RemoteAttribute)
{
if (validator.ErrorMessageResourceType != typeof(CommonLanguage.Language))
{
//var RemoteAttr = validator as System.Web.Mvc.RemoteAttribute;
validator.ErrorMessageResourceType = typeof(CommonLanguage.Language);
validator.ErrorMessageResourceName = "RemoteAttribute_NoUrlFound";
}
allAttributes.Add(validator);
continue;
}
}
return base.GetValidators(metadata, context, allAttributes);
}
}