如何为每个程序集定义一次验证属性的资源类型?

发布于 2024-12-19 05:59:39 字数 551 浏览 3 评论 0原文

目前,我正在设置域模型以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

行雁书 2024-12-26 05:59:39

您可以编写一个自定义的 DataAnnotationsModelValidator:

public class GlobalResourceTypeResourceDataAnnotationsModelValidator : DataAnnotationsModelValidator<ValidationAttribute>
{
    public GlobalResourceTypeResourceDataAnnotationsModelValidator(
        ModelMetadata metadata, 
        ControllerContext context, 
        ValidationAttribute attribute
    )
        : base(metadata, context, attribute)
    {
        if (Attribute.ErrorMessageResourceType == null)
        {
            Attribute.ErrorMessageResourceType = typeof(ModelValidationMessages);
        }
    }
}

然后在 Application_Start 中您需要为您使用的每个属性注册一个适配器:

DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(RequiredAttribute), typeof(GlobalResourceTypeResourceDataAnnotationsModelValidator));
DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(StringLengthAttribute), typeof(GlobalResourceTypeResourceDataAnnotationsModelValidator));
...

You could write a custom DataAnnotationsModelValidator:

public class GlobalResourceTypeResourceDataAnnotationsModelValidator : DataAnnotationsModelValidator<ValidationAttribute>
{
    public GlobalResourceTypeResourceDataAnnotationsModelValidator(
        ModelMetadata metadata, 
        ControllerContext context, 
        ValidationAttribute attribute
    )
        : base(metadata, context, attribute)
    {
        if (Attribute.ErrorMessageResourceType == null)
        {
            Attribute.ErrorMessageResourceType = typeof(ModelValidationMessages);
        }
    }
}

and then in Application_Start you need to register an adapter for each attribute you use:

DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(RequiredAttribute), typeof(GlobalResourceTypeResourceDataAnnotationsModelValidator));
DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(StringLengthAttribute), typeof(GlobalResourceTypeResourceDataAnnotationsModelValidator));
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文