SQL Server PutAsJsonAsync not working (404 error) BUT GetFromJsonAsync working from same cantroller

zlwx9yxi  于 2023-03-22  发布在  其他
关注(0)|答案(1)|浏览(130)

I am developing a Blazor WASM app, and have been experiencing 404 errors making a HTTP PutAsJsonAsync call to a controller. The GetFromJsonAsync call to the same controller is working, from the same Index.razor file. This tells me that it is potentially my syntax or something I have set incorrectly in a different file, although I have followed numerous examples to no avail.

The controller code is as follows:

using moovpad.Shared;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.AspNetCore.Mvc;
namespace moovpad.Server.Controllers
    {
         [ApiController]
         [Route("api/[controller]")]
         public class m0000auController : ControllerBase
         {
             private readonly mpauDbContext db;

             public string ID; 

         public m0000auController(mpauDbContext db)
         {
             this.db = db;
         }

        [HttpPut("ID")]
        public async Task<m0000au> Put(string ID, [FromBody] m0000au moov)
        {
            var edit = await db.m0000au.FindAsync(ID);
            if (edit != null)
            {
                edit.uID = moov.uID;
                edit.moovID01 = moov.moovID01;
                edit.moovID01 = moov.moovID02;
                edit.moovID01 = moov.moovID03;
                edit.moovID01 = moov.moovID04;
                edit.moovID01 = moov.moovID05;
                edit.moovID01 = moov.moovID06;
                edit.moovID01 = moov.moovID07;
                edit.moovID01 = moov.moovID08;
                edit.moovID01 = moov.moovID09;
                edit.moovID01 = moov.moovID10;
                await db.SaveChangesAsync();
            }
            return edit;
        }

        [HttpDelete("id")]
        public async Task<m0000au> Delete(string id)
        {
            var delete = await db.m0000au.FindAsync(id);
            if (delete != null)
            {
                db.m0000au.Remove(delete);
                await db.SaveChangesAsync();
            }
            return delete;
        }

        [HttpPost]
        public async Task<m0000au> Post([FromBody] m0000au create)
        {
            create.uID = ID;
            EntityEntry<m0000au> moov = await db.m0000au.AddAsync(create);
            await db.SaveChangesAsync();
            return moov.Entity;
        }

        [HttpGet]
        public async Task<m0000au> Get(string ID)
        {
            return await Task.Factory.StartNew<m0000au>(() =>
            {
                if (string.IsNullOrEmpty(ID))
                    return null;
                else
                    return db.m0000au.Find(ID);
            });
        }

        [HttpGet("id")]
        public async Task<m0000au> Get(Guid id)
        {
            return await db.m0000au.FindAsync(id);
        }
    }
}

The calls from the Index.razor file are as follows:

public async Task Save()
    {
        var stg1MoovWrite = false;
        //var stg2MoovWrite = false;
        //var stg3MoovWrite = false;

        //HTTP REQUEST FOR CURRENTLY STORED EXERCISES/MOOVs
        var storedMoovIDs = new m0000au();
        storedMoovIDs = await Http.GetFromJsonAsync<m0000au>($"api/m0000au?ID={Uri.EscapeDataString(sesn.usr.uID)}");

        //HTTP PUT DATA - since already have a row in the table with the user's ID as the key
        if (storedMoovIDs != null)
        {
            for (i = 0; i < 10; i++)
            {
                if (!stg1MoovWrite)
                {
                    switch (i)
                    {
                         case 0:
                            if (storedMoovIDs.moovID01 == "n")
                            {
                                // *** TEST CASE WAS PUT INTO THIS LOOP BUT REMOVED FOR POSTING

                                storedMoovIDs.moovID01 = sesn.moov.moovID;
                                var response = await Http.PutAsJsonAsync<m0000au>($"api/m0000au?ID={Uri.EscapeDataString(sesn.usr.uID)}", storedMoovIDs);
                            if (response.IsSuccessStatusCode) stg1MoovWrite = true;

                            }
                        break;
                    }
                }
            }
         }   
    }

I have tried to follow online examples and tutorials, all of which have used very similar syntax to each other, and the syntax I've used follows those guides (at least I believe so, as I can't spot obvious differences). Also, although I can't debug in VS and step through the code step-by-step, I have tested using the loading of a different file/page in the IF LOOP marked within the 'case 0' of the SWITCH LOOP. The page successfully loaded, meaning that the GET call was successful, and this was also confirmed in Chrome debug console where the output showed a 200 response for the GET but constantly getting 404 response for the PUT call.

Any advice on my syntax or other potential causes would be very much appreciated, with thanks in advance for your time.

bnl4lu3b

bnl4lu3b1#

The issue is using [HttpPut("ID")] . ID (as a string) becomes part of the route. The final route to this action becomes:

api/m0000au/ID?ID=100 (notice the extra ID string)

[HttpPut("test")] would result in api/m0000au/test?ID=100 route.

If you change to [HttpPut] then api/m0000au?ID=100 will work as expected.

Maybe your intention was to use [HttpPut("{ID}")] (notice the brackets)

This means that the ID parameter will come from the route instead of query string parameter: api/m0000au/100

Check ASP.NET Core Routing

相关问题