-2

I am new to Blazor and I have a question about Blazor and API.

In my .razor file, I have this code where I am able to get the data to fill in in my table (so this code works):

@code {
    public PrgUser[] users { get; set; }
    IList<PrgUser> selectedUser;

    protected override async Task OnInitializedAsync()
    {
        users = await Http.GetFromJsonAsync<PrgUser[]>("http://localhost:30958/api/Test");

        selectedUser = users.Take(1).ToList();
    }
}

And this code works great with the following code on the API side:

[Route("api/[controller]")]
[ApiController]
public class TestController : ControllerBase
{
    private readonly IneosAppDevContext _context;

    public TestController(IneosAppDevContext context)
    {
        _context = context;
    }

    // GET: api/Areas
    [HttpGet]
    public async Task<ActionResult<IEnumerable<PrgUser>>> GetUser()
    {
        return await _context.PrgUser.ToListAsync();
    }
}

But now I want do add a DELETE function but I can't figure out how to do this. I have this code, but it doesn't work this way...

.razor file:

  @code {
    public PrgUser[] users { get; set; }
    IList<PrgUser> selectedUser;

    protected override async Task OnInitializedAsync()
    {
        users = await Http.GetFromJsonAsync<PrgUser[]>("http://localhost:30958/api/Test");

        selectedUser = users.Take(1).ToList();
    }


    protected async Task Enter()
    {
        Console.WriteLine("Pressed");
        var response = await Http.DeleteAsync("http://localhost:30958/api/Test/" + selectedUser[0].UserId);
    }
 }

API side:

    [HttpDelete]
    [Route("/api/Test/{id}")]

    public async Task<HttpResponseMessage> Delete(int id)
    {
        var user = await _context.PrgUser.FindAsync(id);

        if (user == null)
        {
            // if ID not found
            return new HttpResponseMessage(HttpStatusCode.NotFound);
        }

        try 
        {
            // Success
            _context.PrgUser.Remove(user);
            return new HttpResponseMessage(HttpStatusCode.OK);

        }
        catch (Exception ex)
        {
            // Exception
            return new HttpResponseMessage(HttpStatusCode.ExpectationFailed);
        }
  • "I have this code, but it doesn't work this way..." -- what do you mean? In what way does it appear to not work. What code is getting executed? – Kirk Woll Nov 16 '21 at 17:14
  • Provide all error messages and state actual vs expected results. – H H Nov 17 '21 at 06:27
  • @KirkWoll I tried using that code but it doesn't work. My question is if someone know my fault. – Brecht_Mels Nov 18 '21 at 12:23

1 Answers1

0

Assuming the API controller method gets hit.

The signature for the controller function should look like this:

[HttpDelete]
[Route("/api/Test/{id}")]
public async Task<HttpResponseMessage> Delete(int id)
{

    // if ID not found     
    return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Id " + id.ToString() + "not found");
  
   // Success
    return Request.CreateResponse(HttpStatusCode.OK);  

   // Exception
   return Request.CreateErrorResponse(HttpStatusCode.BadRequest, Ex);  
}

and in the call

var response = await Http.DeleteAsync("http://localhost:30958/api/Test/" + selectedUser[0].UserId);
// Check the response and act accordingly
MrC aka Shaun Curtis
  • 19,075
  • 3
  • 13
  • 31
  • Thank you for take a look at my code and try to help me! I really appreciate it! Can u take a look at my code again i edited with your info. In my API side i used a title different code because i had the same problem as in this post (https://stackoverflow.com/questions/41757245/request-createresponse-in-asp-net-core) But when i try to run it now still nothing is done in de database. Can you see any other problem in my code ? – Brecht_Mels Nov 18 '21 at 12:49
  • In debug mode check that the Controller is actually getting hit, – MrC aka Shaun Curtis Nov 18 '21 at 14:24