示例
1、AccountController.cs中添加验证邮箱的方法
public async Task<IActionResult> IsEmailAlreadyRegistered(string email)
{
ApplicationUser? applicationUser = await
_userManager.FindByEmailAsync(email);
if (applicationUser != null)
{
return Json(false); //invalid
}
else
{
return Json(true); //valid
}
}
2、安装NuGet包
<PackageReference Include="Microsoft.AspNetCore.Mvc.ViewFeatures"
Version="2.2.0" />
3、RegisterDTO.cs中Email字段添加Remote DataAnnotation
[Required(ErrorMessage = "Email can't be blank")]
[EmailAddress(ErrorMessage = "Email should be in a proper email address
format")]
[Remote(action: "IsEmailAlreadyRegistered", controller:"Account", ErrorMessage =
"Email is already exists!")]
public string Email { get; set; }
程序运行后,注册一个已经注册的Email,按Tab键即可看到已存在提示
Gitee获取源码: