简体   繁体   中英

Returned 404 Not Found when request user email [ASP.NET Web API]

I'm trying to get user email from ASP.NET Web API through writing an email in an input box using JQuery (to select message recipients), But the returned value is null. Are there any problems in passing process in my code?

Web API:

[AllowAnonymous]
        [HttpGet]
        [Route("get-reciver-email")]
        public ApplicationUser GetReciverEmailId([FromUri] string email)
        {
            return _userManager.FindByEmail(email);

        }

Client-side "JQuery":

$('#toEmail').keyup(function () {
                $.ajax({
                    url: '/api/Account/get-reciver-email/' + $("#toEmail").val(),
                    method: 'GET',
                    success: function (response) {
                        console.log(response);
                    },
                    // Display errors if any in the Bootstrap alert <div>
                    error: function (jqXHR) {
                        $('#divErrorText').text(jqXHR.responseText);
                        $('#divError').show('fade');
                    }
                });
            });

I expect to return user info based on passed email in HTML form.

Solved.

I removed [FromUri] & I used query-string (THX for @Mihai), also I modified API function to the following:

[AllowAnonymous]
[HttpGet]
[Route("get-reciver-email")]
public string GetReciverEmailId(string email)
{
    return   db.Users.Where(x => x.Email == email).Select(x => x.Id).Single();

}

Client-side code:

$('#toEmail').keyup(function () {
                $.ajax({
                    url: '/api/Account/get-reciver-email/?email=' + $("#toEmail").val(),
                    method: 'GET',
                    success: function (response) {
                        console.log(response);
                    },
                    // Display errors if any in the Bootstrap alert <div>
                    error: function (jqXHR) {
                        $('#divErrorText').text(jqXHR.responseText);
                        $('#divError').show('fade');
                    }
                });
            });

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