我创建了一个自定义ResourceProvider
来从数据库中提取本地化信息.我现在想使用 DataAnnotation
为模型添加验证.
I have created a custom ResourceProvider
to pull localization information from a database. I now want to use DataAnnotation
to add validation to the model.
DataAnnotation
具有 ErrorMessageResourceType
和 ErrorMessageResourceName
属性,但 ErrorMessageResourceType
只接受 System.Type
(即编译好的资源文件)
DataAnnotation
has ErrorMessageResourceType
and ErrorMessageResourceName
properties but ErrorMessageResourceType
only accepts System.Type
(i.e. a compiled resource file)
有没有办法让 DataAnnotation 使用自定义的 ResourceProvider?
Is there any way to get DataAnnotation to use the custom ResourceProvider?
我意识到这是一个老问题,但想补充一点.我发现自己处于同样的情况,似乎没有关于这个主题的任何文档/博客.尽管如此,我还是想出了一种使用自定义资源提供程序的方法,但需要注意的是.需要注意的是,我在一个 MVC 应用程序中,所以我仍然有 HttpContext.GetLocalResourceObject()
可用.这是 asp.net 用来本地化项目的方法.资源对象的缺失不会阻止您编写我们自己的解决方案,即使它是直接查询数据库表.不过,我认为值得指出.
I realize this is an old question, but wanted to add a bit. I found myself in the same situation and there doesn't appear to be any documentation/blogumentation on this topic. Nevertheless, I figured out a way to use a custom resource provider, with one caveat. The caveat is that I'm in an MVC application so I still have HttpContext.GetLocalResourceObject()
available. This is the method that asp.net uses to localize items. The absence of the resource object doesn't stop you from writing our own solution, even if its a direct query of the DB tables. Nevertheless, I thought it was worth pointing out.
虽然我对以下解决方案不太满意,但它似乎有效.对于我想使用的每个验证属性,我都从所述属性继承并重载 IsValid().装修是这样的:
While I'm not terribly happy with the following solution, it seems to work. For each validation attribute I want to use I inherit from said attribute and overload the IsValid(). The decoration looks like this:
[RequiredLocalized(ErrorMessageResourceType= typeof(ClassBeginValidated), ErrorMessageResourceName="Errors.GenderRequired")]
public string FirstName { get; set; }
新属性如下所示:
public sealed class RequiredLocalized : RequiredAttribute {
public override bool IsValid(object value) {
if ( ! (ErrorMessageResourceType == null || String.IsNullOrWhiteSpace(ErrorMessageResourceName) ) ) {
this.ErrorMessage = MVC_HtmlHelpers.Localize(this.ErrorMessageResourceType, this.ErrorMessageResourceName);
this.ErrorMessageResourceType = null;
this.ErrorMessageResourceName = null;
}
return base.IsValid(value);
}
}
注意事项
(半被盗的)帮助代码看起来像这样......
The (semi-stolen) helper code looks like this ....
public static string Localize (System.Type theType, string resourceKey) {
return Localize (theType, resourceKey, null);
}
public static string Localize (System.Type theType, string resourceKey, params object[] args) {
string resource = (HttpContext.GetLocalResourceObject(theType.FullName, resourceKey) ?? string.Empty).ToString();
return mergeTokens(resource, args);
}
private static string mergeTokens(string resource, object[] args) {
if (resource != null && args != null && args.Length > 0) {
return string.Format(resource, args);
} else {
return resource;
}
}
这篇关于带有自定义 ResourceProvider 的 DataAnnotation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!