1

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.

floDV1996
  • 17
  • 5
  • 1
    The first place to avoid duplicat Mails is in the Database where you store stuff. The column should be marked "unique". The rest is just reacting to the DB rejecting the INSERT command. Any check in you application is prone to run into race conditons, especially with Web Applications that have multiple clients. – Christopher Feb 09 '18 at 22:34
  • You might find this resource useful http://tutlane.com/tutorial/aspnet-mvc/remote-validations-in-asp-net-mvc-4-with-example – mike123 Feb 09 '18 at 22:39
  • What problems did you run in to with `[Remote]`? – Kirk Larkin Feb 09 '18 at 22:46
  • I used the [Remote] in my client viewModel but I wanted to access the action in my API. So it didn't work. – floDV1996 Feb 10 '18 at 07:23

1 Answers1

1

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.

Lost_In_Library
  • 3,265
  • 6
  • 38
  • 70