Reputation: 2385
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);
}
}
Upvotes: 3
Views: 2647
Reputation: 247133
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.
Upvotes: 4
Reputation: 157
You have to tell the router about your api signature.. now by replacing [HttpDelete("{id}")]
by [HttpDelete]
your signature become api/[controller]/[action]
, thus your router will ignore anything after that signature.
You can define custom route if you like to leave it as [HttpDelete]
where you will also specify id as a parameter.
Upvotes: 0