通过使用客户端 (MVC) 网络中的操作来验证电子邮件是否存在于具有远程验证的数据库中 API?

Verify if email exists in database with remote validation by consuming with the client (MVC) the action in the web API?

我有一个网站 API (ASP.net),我想要 2 个客户端(Xamarin 和 ASP.net MVC)。

我的网站 API 有效,我用 Postman 测试了它。我尝试通过使用 WebAPI 使用 Web 客户端添加新用户并且它有效。但是现在我想检查当用户在表单中输入他的电子邮件时数据库中是否已经存在电子邮件。

我看到可以使用 来执行此操作,但此解决方案对我不起作用。

如果电子邮件存在,我可以查看 API。

    [AllowAnonymous]
    [HttpGet]
    public async Task<JsonResult<bool>> VerifyEmailAsync(string email)
    {
        var user = await UserManager.FindByEmailAsync(email);

        if (user == null)
        {
            return Json(false);
        }
        else
        {
            return Json(true);
        }
    }

我可以检索 true 或 false,但我不知道如何在我的 MVC 客户端中使用它。

希望您能理解我的问题。

如果您从 VerifyEmailAsync 方法中 return 布尔值 (true/false) 是不可能的。如果将结果更改为 Task<JsonResult>

,则远程属性将起作用

所以我推荐;你可以 return 这样的 http 响应代码;

所以你会更接近web api standards. But if you have a lot of business messages like this, you could think json-envelope方法。