如何为每个程序集定义一次验证属性的资源类型?
目前,我正在设置域模型以使用 DataAnnotation 验证属性,例如RequiredAttribute 和RangeAttribute。
对于一个属性,它看起来像这样:
[Required(ErrorMessageResourceType = typeof(ModelValidationMessages), ErrorMessageResourceName = "SurnameRequiredMessage")]
public string Surname { get; set; }
但是,这不是唯一的属性,更不用说唯一的模型类了。但是,验证消息仅在整个程序集的 ErrorMessageResourceType 资源类中列出。
问题:
- 是否可以为整个类定义 ErrorMessageResourceType 属性?
- 是否可以为整个程序集定义 ErrorMessageResourceType 属性?
- 如果不行还有其他办法吗?
提前致谢!
Currently I'm setting up my domain model to use DataAnnotation validation attributes, like the RequiredAttribute and RangeAttribute.
For one property it looks like this:
[Required(ErrorMessageResourceType = typeof(ModelValidationMessages), ErrorMessageResourceName = "SurnameRequiredMessage")]
public string Surname { get; set; }
However this isn't the only property, let alone the only model class. The validation messages, however, are only listed only in the ErrorMessageResourceType resource class for the entire assembly.
Questions:
- Is it possible to define the ErrorMessageResourceType property for an entire class?
- Is it possible to define the ErrorMessageResourceType property for an entire assembly?
- If not, any other way?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以编写一个自定义的 DataAnnotationsModelValidator:
然后在 Application_Start 中您需要为您使用的每个属性注册一个适配器:
You could write a custom
DataAnnotationsModelValidator
:and then in Application_Start you need to register an adapter for each attribute you use: