我需要发帖或在服务器端进行验证以检查电子邮件是否唯一。
在研究中,我一直做的例子是一个传统的 MVC 应用程序,而不是一个 api。
在许多情况下,我看到 [Remote] https://docs.microsoft.com/pt-br/aspnet/core/mvc/models/validation?view=aspnetcore-2.2#remote-attribute。我尝试按照文档来实现,但是调试验证了控制器中的函数没有被调用。
用户.cs
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.AspNetCore.Mvc;
namespace Base.Models
{
[Table("users")]
public partial class User
{
...
[Required]
[EmailAddress]
[Remote(action: "VerifyEmail", controller: "UserController",ErrorMessage="Email already in use")]
[Column("email", TypeName = "varchar(254)")]
public string Email { get; set; }
...
}
}
Run Code Online (Sandbox Code Playgroud)
用户控制器.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Base.Models;
namespace Base.Controllers
{
[Route("api/users")]
[ApiController]
public class UserController : Controller
{
... …Run Code Online (Sandbox Code Playgroud)