Click here to Skip to main content
15,041,266 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all
I have been able to call my api with Create, Get, GetAll, and Delete. But I am having troubles with only Update. I thought it would be the same as Create but It seems not. I tried with HttpPut and HttpPost but nether called my API method.

C#
// Here is my MVC View Controller that calls the API.
public async Task<IActionResult> UpdateAppUser(string guid)
        {
            AppUser user = new AppUser();
            HttpRequestMessage httprequest = new HttpRequestMessage(HttpMethod.Get, $"https://localhost:5001/api/account/GetUserByID/{guid}");
            // httprequest.Content = new StringContent(guid);

            var client = apiClient.CreateClient();
            var response = await client.SendAsync(httprequest);

            if (response.IsSuccessStatusCode)
            {
                var responseString = await response.Content.ReadAsStringAsync();
                user = JsonConvert.DeserializeObject<AppUser>(responseString);
               // return View("AppUser", user);
            }/**/


            return View(user);
        }
        //[Bind("FirstName,LastName,Email")]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> UpdateAppUser(AppUser user)
        {
            if (ModelState.IsValid)
            {
                RegisterModel registerModel = new RegisterModel
                {
                    Username = user.userName,
                    FirstName = user.firstName,
                    LastName = user.lastName,
                    DateCreated = user.dateCreated,

                };
                //var model = JsonConvert.SerializeObject(user);
                var model = JsonConvert.SerializeObject(registerModel);

                HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Post, $"https://localhost:5001/api/account/UpdateUser/");
                httpRequest.Content = new StringContent(model, Encoding.UTF8, "application/json");

                var client = apiClient.CreateClient();
                var response = await client.SendAsync(httpRequest);
                
                if (response.IsSuccessStatusCode)
                {
                    return View("AppUserIndex");
                }
            }
            return View(user);
        }
// Here is the API
[HttpPost("UpdateUser")]
        //[Route("UpdateUser")]
        public async Task<IActionResult> UpdateUser(RegisterModel registerModel)
        {
            if (ModelState.IsValid)
            {
                var user = await userManager.FindByEmailAsync(registerModel.Email);
                if (user != null)
                {
                    user.Email = registerModel.Email;
                    user.UserName = registerModel.Username;
                    user.FirstName = registerModel.FirstName;
                    user.LastName = registerModel.LastName;

                    IdentityResult validEmail = null;
                    if (!string.IsNullOrEmpty(registerModel.Email))
                    {
                      //  validEmail = await 
                    }

                    IdentityResult identityResult = await userManager.UpdateAsync(user);
                    if (identityResult.Succeeded)
                    {
                        return Ok();
                    }
                }
                else
                    return NotFound();
            }
            return BadRequest();
        }


What I have tried:

I have tried using httpput, httppost, using the same way I did with Create and a bunch of other ways to get this to work. This is the only 1 i have left until I can work on Login and Creating Tokens, etc for my user microservice.
Posted
Updated 27-Dec-20 9:57am

1 solution

Create implies "new", not update; which involves at least one read and one write; unless one is just blindly posting; overwriting whatever gets in the way.

Any "updating" would happen in the model / dal / orm / back-end ... Your "calls" have no effect unless there is "db code" to recognize it; which you do not show.

On the other, a "delete" and a "create" could substitute as an update; though that's just about a last resort for any number of reasons. Then again, there could be a valid business reason for "no updates"; just (re)cycling ...
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900