Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete request without id parameter in .net webApi

I have .net core WebApi like below. And it is working perfectly. But, when I write [HttpDelete] instead of [HttpDelete("{id}")] , then it doesn't work. What can be reason ?

My url : http://localhost:5004/api/Student/DeleteStudent/23

[ApiController]
[Route("api/[controller]/[action]")]
public class StudentController : ControllerBase
{
    //[HttpDelete] ///////////////// This is not working
    [HttpDelete("{id}")] /////////// This is working
    public async Task<ServiceResult> DeleteStudent(int id)
    {
      return await studentService.DeleteStudent(id);
    }
}
like image 223
realist Avatar asked Mar 05 '26 13:03

realist


1 Answers

Without the {id} route template parameter, the only other way to populate the value would be via query string

http://localhost:5004/api/Student/DeleteStudent?id=23 

The route table will match the querystring parameter to the action parameter to make the match.

Either way, the id needs to be provided to know which action to call and which record to delete.

like image 119
Nkosi Avatar answered Mar 08 '26 01:03

Nkosi