[英]Verify if email exists in database with remote validation by consuming with the client (MVC) the action in the web API?
I have a web API (ASP.net) and I want to have 2 clients (Xamarin and ASP.net MVC). 我有一个Web API(ASP.net),我想拥有2个客户端(Xamarin和ASP.net MVC)。
My web API works, I tested it with Postman. 我的Web API正常运行,我用Postman对其进行了测试。 I tried to add a new user with the web client by consuming the WebAPI and it works. 我尝试通过使用WebAPI向Web客户端添加新用户,并且该方法有效。 But now I want to check if email already exists in the database when the user enters his email in the form. 但是现在我想检查用户在表单中输入电子邮件时数据库中是否已经存在电子邮件。
I saw that it's possible to use a RemoteAttribute to do this but this solution does not work for me. 我看到可以使用RemoteAttribute来做到这一点,但是这种解决方案对我不起作用。
I can check in the API if the email exists. 我可以检查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);
}
}
I can retrieve true or false but I don't know how I can consume this in my MVC client. 我可以获取true或false,但我不知道如何在MVC客户端中使用它。
I hope you understand my problem. 希望你能理解我的问题。
It's not possible if you return boolean (true/false) from your VerifyEmailAsync
method. 如果从VerifyEmailAsync
方法返回布尔值(真/假),则是VerifyEmailAsync
。 Remote attribute will work if you change the result as Task<JsonResult>
如果将结果更改为Task<JsonResult>
则远程属性将起作用
So what I recommend; 所以我建议 you can return http response code like this; 您可以像这样返回http响应代码;
HTTP response code for POST when resource already exists 资源已经存在时POST的HTTP响应代码
So you will be closer to web api standards . 因此,您将更接近Web api标准 。 But if you have a lot of business messages like this, you could think json-envelope approach. 但是,如果您有很多这样的业务消息,则可以考虑使用json-envelope方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.