C# ASP.NET 核心 - Data.Annotations - 特定电子邮件的正则表达式

C# ASP.NET Core - Data.Annotations - Regex for specific email

如何通过 ASP.NET 核心中的数据注释将此正则表达式 \b[A-Za-z0-9._%-]+@(live\.wcs\.ac\.uk)\b 实施为正则表达式检查。

[Required]    
[RegularExpression(\b[A-Za-z0-9._%-]+@(live\.wcs\.ac\.uk)\b)]    
public string Email {get; set; }

这就是我尝试设置它的方式,因为我希望将 @live.wcs.ac.uk 作为验证检查,但我的 IDE 不喜欢我在上面尝试的输入。

任何有关如何正确设置注释的快速解释的帮助?

C# 中的正则表达式应声明为 string 对象,即:

[RegularExpression(@"\b[A-Za-z0-9._%-]+@(live\.wcs\.ac\.uk)\b")]

我认为如果你使用可能会更好:

[EmailAddress(ErrorMessage = "your error message here")]

更多信息here