Asp.net mvc 5 用于验证ASP.net MVC5中ApplicationUser电子邮件地址的正则表达式

Asp.net mvc 5 用于验证ASP.net MVC5中ApplicationUser电子邮件地址的正则表达式,asp.net-mvc-5,Asp.net Mvc 5,我正在asp.net MVC5中创建一个webapp,它只允许具有特定域的用户注册 据我所知,最简单的方法是使用正则表达式。是否有任何方法可以使用数据注释来更改模型,或者我必须使用视图来实现此目的?对于MVC 5,您使用的是ASPNET Identiy,并且基本属性封装在IdentityUser中,因此对于您的情况,您不能直接访问字段电子邮件,但您可以使用EF Fluent API添加一些规则,但部分原因是regularexpression属性不是EF Fluent API的一部分 如果您需要了

我正在asp.net MVC5中创建一个webapp,它只允许具有特定域的用户注册


据我所知,最简单的方法是使用正则表达式。是否有任何方法可以使用数据注释来更改模型,或者我必须使用视图来实现此目的?

对于MVC 5,您使用的是ASPNET Identiy,并且基本属性封装在IdentityUser中,因此对于您的情况,您不能直接访问字段电子邮件,但您可以使用EF Fluent API添加一些规则,但部分原因是regularexpression属性不是EF Fluent API的一部分

如果您需要了解如何将EF Fluent API与ASPNET标识结合使用:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    { }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        //Define rules
        modelBuilder.Entity<IdentityUser>()
        .Property(u => u.Email).IsRequired();
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}
public类ApplicationDbContext:IdentityDbContext
{
公共应用程序上下文()
:base(“DefaultConnection”,throwifvv1schema:false)
{ }
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
基于模型创建(modelBuilder);
//定义规则
modelBuilder.Entity()
.Property(u=>u.Email).IsRequired();
}
公共静态应用程序上下文创建()
{
返回新的ApplicationDbContext();
}
}

关于,

对于MVC 5,您使用的是ASPNET Identiy,基本属性封装在IdentityUser中,因此对于您的情况,您无法直接访问字段电子邮件,但您可以使用EF Fluent API添加一些规则,但部分是regularexpression属性不是EF Fluent API的一部分

如果您需要了解如何将EF Fluent API与ASPNET标识结合使用:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    { }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        //Define rules
        modelBuilder.Entity<IdentityUser>()
        .Property(u => u.Email).IsRequired();
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}
public类ApplicationDbContext:IdentityDbContext
{
公共应用程序上下文()
:base(“DefaultConnection”,throwifvv1schema:false)
{ }
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
基于模型创建(modelBuilder);
//定义规则
modelBuilder.Entity()
.Property(u=>u.Email).IsRequired();
}
公共静态应用程序上下文创建()
{
返回新的ApplicationDbContext();
}
}

关于,

现在可能有点晚了,但我建议如下:

我有一个应用程序,我需要在其中修改一些ApplicationUser属性,我正在尝试一些还没有在现实生活中测试过的东西,但是我的单元测试到目前为止都通过了,它可能对你有用

覆盖ApplicationUser中的电子邮件属性,并添加自定义验证批注:

[ValidateEmailDomain(ErrorMessage = "Not a valid email domain.")]    
public override string Email { get; set; }
ValidateEmailDomain代码:

using System.ComponentModel.DataAnnotations;
using System.Web;

namespace WATHEVER
{
    public class ValidateEmailDomain: RequiredAttribute
    {
        public override bool IsValid(object value)
        {
            //code
        }
    }
}
请注意,由于ValidateEmailDomain继承自RequiredAttribute,它显然是必须的,如果您不希望这样做,也可以在它为null时对其进行验证


对不起,我的英语:/

现在可能有点晚了,但我建议如下:

我有一个应用程序,我需要在其中修改一些ApplicationUser属性,我正在尝试一些还没有在现实生活中测试过的东西,但是我的单元测试到目前为止都通过了,它可能对你有用

覆盖ApplicationUser中的电子邮件属性,并添加自定义验证批注:

[ValidateEmailDomain(ErrorMessage = "Not a valid email domain.")]    
public override string Email { get; set; }
ValidateEmailDomain代码:

using System.ComponentModel.DataAnnotations;
using System.Web;

namespace WATHEVER
{
    public class ValidateEmailDomain: RequiredAttribute
    {
        public override bool IsValid(object value)
        {
            //code
        }
    }
}
请注意,由于ValidateEmailDomain继承自RequiredAttribute,它显然是必须的,如果您不希望这样做,也可以在它为null时对其进行验证


对不起,我的英语是://

请在您的属性上使用
[RegularExpression]
属性。电子邮件属性存储在哪里?我在模型中的任何地方都找不到它。请使用属性上的
[RegularExpression]
属性。电子邮件属性存储在哪里?我在模型里什么地方都找不到