简体   繁体   中英

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).

My web API works, I tested it with Postman. I tried to add a new user with the web client by consuming the WebAPI and it works. 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.

I can check in the API if the email exists.

    [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.

I hope you understand my problem.

It's not possible if you return boolean (true/false) from your VerifyEmailAsync method. Remote attribute will work if you change the result as Task<JsonResult>

So what I recommend; you can return http response code like this;
HTTP response code for POST when resource already exists

So you will be closer to web api standards . But if you have a lot of business messages like this, you could think json-envelope approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM