realist
realist

Reputation: 2385

Delete request to ASP.NET Core ApiController not working

I have apiController like below. And, I'm sending delete request by Postman. But, my delete request not access to method. But, get method working perfectly. What can be the reason of this bug?

My postman Url is: http://localhost:5004/api/Student/DeleteStudent/23

[ApiController]
[Route("api/[controller]/[action]")]
public class StudentController : ControllerBase
{
    [HttpDelete("DeleteStudent/{studentId}")]
    public async Task<ServiceResult> DeleteStudent(long studentId)
    {
      return await studentService.DeleteStudent(studentId);
    }

    [HttpGet]
    public async Task<ServiceResult> GetStudents(int studentType)
    {
        return await studentService.GetStudents(studentType);
    }
}

Upvotes: 2

Views: 2133

Answers (1)

TanvirArjel
TanvirArjel

Reputation: 32099

Use [HttpDelete("{studentId}")] instead of [HttpDelete("DeleteStudent/{studentId}")] on DeleteStudent() method as follows:

[HttpDelete("{studentId}")]
public async Task<ServiceResult> DeleteStudent(long studentId)
{
  return await studentService.DeleteStudent(studentId);
}

I have tested it in a test project with Postman and it works perfectly!

Upvotes: 3

Related Questions