如何编写自定义RegularEx pressionValidator从配置文件中所采用的值? [英] How to write custom RegularExpressionValidator which takes the values from the config file?

查看:145
本文介绍了如何编写自定义RegularEx pressionValidator从配置文件中所采用的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用用户名物业的普通前pression验证器在我的模型。
我从配置文件中获取此正前pression。

  [RegularEx pression(UsernameValidationEx pression)] // UsernameValidationEx pression =从配置文件中值
公共字符串用户名{获得;组; }

我在这里得到一个错误
的属性参数必须是常量前pression,属性参数类型的typeof前pression或数组创建前pression

我怎样才能解决这个?


解决方案

 公共类ConfigRegularEx pressionAttribute:RegularEx pressionAttribute
{
    公共ConfigRegularEx pressionAttribute(字符串patternConfigKey)
        :基地(ConfigurationManager.AppSettings [patternConfigKey])
    {}
}

和则:

  [ConfigRegularEx pression(UsernameValidationEx pression)]
公共字符串用户名{获得;组; }

和在web.config中:

 <&的appSettings GT;
    <添加键=UsernameValidationEx pressionVALUE =富巴/>
< /的appSettings>

I have to use a Regular expression validator for username property in my model. I am getting this regular expression from the config file.

[RegularExpression(UsernameValidationExpression)] //UsernameValidationExpression = value from the config file
public string UserName { get; set; }

Here i am getting an error "An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type"

How can i solve this??

解决方案

public class ConfigRegularExpressionAttribute : RegularExpressionAttribute
{
    public ConfigRegularExpressionAttribute(string patternConfigKey)
        : base(ConfigurationManager.AppSettings[patternConfigKey])
    { }
}

and then:

[ConfigRegularExpression("UsernameValidationExpression")]
public string UserName { get; set; }

and in web.config:

<appSettings>
    <add key="UsernameValidationExpression" value="foo bar" />
</appSettings>

这篇关于如何编写自定义RegularEx pressionValidator从配置文件中所采用的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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