我在 ASP.Net MVC 中有一种方法可以在模型属性上使用 RegularExpression 属性来从资源文件或其他类中提取模式字符串属性吗?
当我尝试使用另一个类时,我收到一条消息,指出属性值必须是常量、typeof 表达式或数组。
最终,我试图抽象出其中的一些模式,这样如果某个模式中存在错误,我就不必更新多个文件。
示例:
public class MyModel{
[RegularExpression(StaticPatternClass.EmailPattern, ErrorMessage="invalid email")]
public string email { get; set;}
}
public static class StaticPatternClass{
public static string EmailPattern = @"My pattern here";
}
最佳答案
尝试
公共(public)常量字符串 EmailPattern ....
代替
关于c# - 在 ASP.Net MVC 中集中正则表达式验证模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7013578/