在全球范围内覆盖ASP.Net MVC默认属性错误信息 [英] Globally Override ASP.Net MVC Default Attribute Error Messages

查看:388
本文介绍了在全球范围内覆盖ASP.Net MVC默认属性错误信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用资源,从不同的装配在我MVC5应用覆盖默认的属性错误信息?

How can I use resources from a different assembly to override the default attribute error messages in my MVC5 application?

我的网站被命名空间:Company.Web

My website is namespaced: Company.Web

我的资源组件命名空间:Company.Web.Resources

My resources assembly is namespaced: Company.Web.Resources

我可以很容易地单独本地化属性的错误信息使用:

I can easily localize attribute error messages individually using:

[Required(ErrorMessageResourceName = "PropertyValueRequired", ErrorMessageResourceType = typeof(Company.Web.Resources.Messages))]

但是,由于我们的错误信息始终是必需的,我只想把 [必需] 属性,而无需指定资源名称。我还想覆盖由MVC输出的默认数据类型的信息,你可以不通过一个属性做的。

However, since our error message is always "Required", I'd simply like to put the [Required] attribute without having to specify the resource name. I'd also like to override the default data type messages output by MVC which you can't do via an attribute.

字段{0}必须是最新的。

The field {0} must be a date.

我想成为

无效的日期

我见过的例子,你可以在App_GlobalResources文件资源文件(键 PropertyValueRequired FieldMustBeDate FieldMustBeNumeric ),并设置 ClientDataTypeModelValidatorProvider.ResourceClassKey ,但我已经有一个外部资源组装我想使用。

I've seen examples where you can put the resource files in App_GlobalResources (with keys PropertyValueRequired, FieldMustBeDate, FieldMustBeNumeric) and setting ClientDataTypeModelValidatorProvider.ResourceClassKey, but I already have an external resources assembly I want to use.

我已经用我的Global.asax以下,没有运气的尝试:

I've tried using the following in my Global.asax with no luck:

ClientDataTypeModelValidatorProvider.ResourceClassKey = "Company.Web.Resources.Messages"

我怎样才能做到这一点?任何想法?

How can I accomplish this? Any ideas?

UPDATE(部分解决)

我可以通过创建新的验证适配器和代替默认使用它们根本解决我的基于属性的问题:

I can solve my attribute-based problem simply by creating new validation adapters and using them in lieu of the default:

public class MyRequiredAttributeAdapter : RequiredAttributeAdapter
{
    public MyRequiredAttributeAdapter(ModelMetadata metadata, ControllerContext context, RequiredAttribute attribute)
        : base(metadata, context, attribute)
    {
        if (attribute.ErrorMessage.IsNullOrWhitespace() 
            && attribute.ErrorMessageResourceName.IsNullOrWhitespace() 
            && attribute.ErrorMessageResourceType == null)
        {
            attribute.ErrorMessageResourceType = typeof (Resources.Validation.Messages);
            attribute.ErrorMessageResourceName = "PropertyValueRequired";
        }
    }
}

Global.asax中

Global.asax

DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(RequiredAttribute), typeof(MyRequiredAttributeAdapter));

然而,这仍然让我抓我的头就如何覆盖非空属性,如DateTime和INT的默认数据类型的信息。另外,我相信有一些我无法重写,因为他们是内部(DataTypeAttributeAdapter,CompareAttributeAdapter)。

However, this still leaves me scratching my head on how to override the default data type message for non-null properties such as DateTime and int. Also, I believe there are some I am unable to override because they are internal (DataTypeAttributeAdapter, CompareAttributeAdapter).

推荐答案

这可能是比较晚的,但那么这里就是应该工作主要基于背后的部分解决方案逻辑的解决方案。

This might be quite late, but then here is a solution that should work based primarily on the logic behind your partial solution.


  1. 实现自定义 RequiredAttribute标签为您的项目

public class MyRequiredAttribute : RequiredAttribute
{
    //Your custom code
}


  • 修改你的 MyRequiredAttributeAdapter code,如图所示。请注意,您现在需要从普通的 DataAnnotationsModelValidator 类,它允许你在你的自定义通过继承 MyRequiredAttribute

  • Modify your MyRequiredAttributeAdapter code as shown. Notice that you now need to inherit from the generic DataAnnotationsModelValidator class, which allows you pass in your custom MyRequiredAttribute

    public class MyRequiredAttributeAdapter : DataAnnotationsModelValidator<MyRequiredAttribute>
    {
        public MyRequiredAttributeAdapter(ModelMetadata metadata, ControllerContext context, MyRequiredAttribute attribute)
            : base(metadata, context, attribute)
        {
            if (string.IsNullOrWhiteSpace(attribute.ErrorMessage)
                && string.IsNullOrWhiteSpace(attribute.ErrorMessageResourceName)
                && attribute.ErrorMessageResourceType == null)
            {
                attribute.ErrorMessageResourceType = typeof(Resources.Validation.Messages);
                attribute.ErrorMessageResourceName = "PropertyValueRequired";
            }
        }
    }
    


  • 这添加到Global.asax中(从你有你的部分解决方案是什么修改)

  • Add this to Global.asax (modified from what you have in your partial solution)

    DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(MyRequiredAttribute), typeof(MyRequiredAttributeAdapter));
    


  • 这篇关于在全球范围内覆盖ASP.Net MVC默认属性错误信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆