[英]Verify if email exists in database with remote validation by consuming with the client (MVC) the action in the web API?
我有一个Web API(ASP.net),我想拥有2个客户端(Xamarin和ASP.net MVC)。
我的Web API正常运行,我用Postman对其进行了测试。 我尝试通过使用WebAPI向Web客户端添加新用户,并且该方法有效。 但是现在我想检查用户在表单中输入电子邮件时数据库中是否已经存在电子邮件。
我看到可以使用RemoteAttribute来做到这一点,但是这种解决方案对我不起作用。
我可以检查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
方法返回布尔值(真/假),则是VerifyEmailAsync
。 如果将结果更改为Task<JsonResult>
则远程属性将起作用
所以我建议 您可以像这样返回http响应代码;
资源已经存在时POST的HTTP响应代码
因此,您将更接近Web api标准 。 但是,如果您有很多这样的业务消息,则可以考虑使用json-envelope方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.