如何在运行时设置RegularExpression数据注释的正则表达式参数? [英] How can I set a RegularExpression data annotation's regular expression argument at runtime?

查看:152
本文介绍了如何在运行时设置RegularExpression数据注释的正则表达式参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们管理几个ASP.NET MVC客户端网站,它们都使用如下所示的数据注释来验证客户的电子邮件地址(我没有在这里包含正则表达式,以便于阅读):

  [必需(ErrorMessage =需要电子邮件)
[RegularExpression(@MYREGEX,ErrorMessage =电子邮件地址无效) ]
public string Email {get;组; }

我想做的是集中这个正则表达式,所以如果我们做一个改变为此,所有的网站都会立即接收,我们不必在每个网站中手动更改。



问题是数据的正则表达式参数注释必须是一个常量,所以我不能在运行时从配置文件或数据库中分配一个值(这是我的第一个想法)。



任何人都可以帮助我有一个聪明的解决方案 - 否则,这种替代方法将有助于实现同样的目标?或者这只是要求我们写一个专门的定制验证属性,它将接受非常数值?

解决方案

最简单的方法是编写一个定制的 ValidationAttribute ,它继承自 RegularExpressionAttribute ,所以像这样:

  public class EmailAttribute:RegularExpressionAttribute 
{
public EmailAttribute()
:base(GetRegex())
{}

私有静态字符串GetRegex()
{
// TODO:关闭并获得您的RegEx
return @^ [\w - ] +(\\ \\ [\w - ] +)* @([A-Z0-9 - ] +(\ [A-Z0-9 - ] +。)* \ [AZ] {2,6-}?| (\d {1,3} \。){3} \d {1,3})(?:\d {4})$;
}
}

这样,你仍然保持使用内置正则表达式验证,但您可以自定义它。您只需简单地使用它:

  [电子邮件(ErrorMessage =请使用有效的电子邮件地址)] 

最后,要让客户端验证工作,您只需将以下内容添加到 Application_Start 方法在 Global.asax 中,告诉MVC对此验证器使用正常的正则表达式验证:

  DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(EmailAttribute),typeof(RegularExpressionAttributeAdapter)); 


We manage several ASP.NET MVC client web sites, which all use a data annotation like the following to validate customer email addresses (I haven't included the regex here, for readability):

[Required(ErrorMessage="Email is required")]
[RegularExpression(@"MYREGEX", ErrorMessage = "Email address is not valid")]
public string Email { get; set; }

What I would like to do is to centralise this regular expression, so that if we make a change to it, all of the sites immediately pick it up and we don't have to manually change it in each one.

The problem is that the regex argument of the data annotation must be a constant, so I cannot assign a value I've retrieved from a config file or database at runtime (which was my first thought).

Can anyone help me with a clever solution to this—or failing that, an alternative approach which will work to achieve the same goal? Or does this just require us to write a specialist custom validation attribute which will accept non-constant values?

解决方案

The easiest way is to write a custom ValidationAttribute that inherits from RegularExpressionAttribute, so something like:

public class EmailAttribute : RegularExpressionAttribute
    {
        public EmailAttribute()
            : base(GetRegex())
        { }

        private static string GetRegex()
        {
            // TODO: Go off and get your RegEx here
            return @"^[\w-]+(\.[\w-]+)*@([a-z0-9-]+(\.[a-z0-9-]+)*?\.[a-z]{2,6}|(\d{1,3}\.){3}\d{1,3})(:\d{4})?$";
        }
    }

That way, you still maintain use of the built in Regex validation but you can customise it. You'd just simply use it like:

[Email(ErrorMessage = "Please use a valid email address")]

Lastly, to get to client side validation to work, you would simply add the following in your Application_Start method within Global.asax, to tell MVC to use the normal regular expression validation for this validator:

DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(EmailAttribute), typeof(RegularExpressionAttributeAdapter));

这篇关于如何在运行时设置RegularExpression数据注释的正则表达式参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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