Skip to main content
 首页 » 编程设计

c#之在 ASP.Net MVC 中集中正则表达式验证模式

2024年02月23日28jillzhang

我在 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 ....

代替