ric*_*ert 12 c# localization asp.net-core-mvc asp.net-core
我正在尝试在asp.net core 1.0中的自定义验证属性中实现本地化.这是我简化的viewmodel:
public class EditPasswordViewModel
{
[Required(ErrorMessage = "OldPasswordRequired")]
[DataType(DataType.Password)]
[CheckOldPassword(ErrorMessage = "OldPasswordWrong")]
public string OldPassword { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
"OldPasswordRequired"的本地化工作正常.但是,我的自定义属性的本地化不起作用,并始终返回"OldPasswordWrong"消息.这是代码:
public class CheckOldPasswordAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(object classInstance, ValidationContext validationContext)
{
if (oldPasswordSaved == oldPasswordTyped) //simplified
{
return ValidationResult.Success;
}
else
{
string errorMessage = FormatErrorMessage(ErrorMessageString);
return new ValidationResult(errorMessage);
}
}
}
Run Code Online (Sandbox Code Playgroud)
ErrorMessageString始终为"OldPasswordWrong",FormatErrorMessage始终返回"OldPasswordWrong".我究竟做错了什么?我正在使用新的asp.net核心数据注释本地化,所以我没有使用ErrorMessageResourceName和ErrorMessageResourceType属性(我没有任何ViewModel.Designer.cs).
Ram*_*min 15
实现适配器以进行本地化:
public class RequiredIfAttributeAdapter : AttributeAdapterBase<RequiredIfAttribute>
{
public RequiredIfAttributeAdapter(RequiredIfAttribute attribute, IStringLocalizer stringLocalizer) : base(attribute, stringLocalizer) {}
public override void AddValidation(ClientModelValidationContext context) {}
public override string GetErrorMessage(ModelValidationContextBase validationContext)
{
return GetErrorMessage(validationContext.ModelMetadata, validationContext.ModelMetadata.GetDisplayName());
}
}
Run Code Online (Sandbox Code Playgroud)
实现适配器的提供程序:
public class CustomValidationAttributeAdapterProvider : IValidationAttributeAdapterProvider
{
private readonly IValidationAttributeAdapterProvider _baseProvider = new ValidationAttributeAdapterProvider();
public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer)
{
if (attribute is RequiredIfAttribute)
return new RequiredIfAttributeAdapter(attribute as RequiredIfAttribute, stringLocalizer);
else
return _baseProvider.GetAttributeAdapter(attribute, stringLocalizer);
}
}
Run Code Online (Sandbox Code Playgroud)
在Startup.cs中注册提供程序:
services.AddSingleton<IValidationAttributeAdapterProvider, CustomValidationAttributeAdapterProvider>();
Run Code Online (Sandbox Code Playgroud)
此博客的致谢:https://blogs.msdn.microsoft.com/mvpawardprogram/2017/01/03/asp-net-core-mvc/
小智 5
Ramin 的答案是正确的答案。但是我决定走另一条路,所以很多情况下我不必编写适配器和适配器提供程序。
这个想法是将您的特定字符串本地化器包装在服务接口中,并从验证属性本身中获取它。
public class CPFAttribute: ValidationAttribute
{
public CPFAttribute()
{
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
string cpf;
try
{
cpf = (string)value;
}
catch (Exception)
{
return new ValidationResult(GetErrorMessage(validationContext));
}
if (string.IsNullOrEmpty(cpf) || cpf.Length != 11 || !StringUtil.IsDigitsOnly(cpf))
{
return new ValidationResult(GetErrorMessage(validationContext));
}
return ValidationResult.Success;
}
private string GetErrorMessage(ValidationContext validationContext)
{
if (string.IsNullOrEmpty(ErrorMessage))
{
return "Invalid CPF";
}
ErrorMessageTranslationService errorTranslation = validationContext.GetService(typeof(ErrorMessageTranslationService)) as ErrorMessageTranslationService;
return errorTranslation.GetLocalizedError(ErrorMessage);
}
}
Run Code Online (Sandbox Code Playgroud)
然后可以将服务创建为:
public class ErrorMessageTranslationService
{
private readonly IStringLocalizer<SharedResource> _sharedLocalizer;
public ErrorMessageTranslationService(IStringLocalizer<SharedResource> sharedLocalizer)
{
_sharedLocalizer = sharedLocalizer;
}
public string GetLocalizedError(string errorKey)
{
return _sharedLocalizer[errorKey];
}
}
Run Code Online (Sandbox Code Playgroud)
该服务可以在 Startup 类中注册为单例。
services.AddSingleton<ErrorMessageTranslationService>();
Run Code Online (Sandbox Code Playgroud)
如果需要将这些验证属性分解到另一个程序集,只需为此翻译服务创建一个接口,您创建的所有验证属性都可以引用该接口。
| 归档时间: |
|
| 查看次数: |
4163 次 |
| 最近记录: |