2

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);
    }
}
TanvirArjel
  • 30,049
  • 14
  • 78
  • 114
realist
  • 2,155
  • 12
  • 38
  • 77

1 Answers1

3

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!

TanvirArjel
  • 30,049
  • 14
  • 78
  • 114
  • Is your url like this? @TanvirArjel http://localhost:5004/api/Student/DeleteStudent/23 – realist Nov 27 '18 at 11:06
  • Yes! It is. URL should be like that! Moreover don't forget to select request Delete on postman! – TanvirArjel Nov 27 '18 at 11:07
  • Great! to hear that it solved your problem! Thank you too. – TanvirArjel Nov 27 '18 at 11:09
  • Can I remove [HttpDelete("{studentId}")]. Because, it is repeating everywhere. @TanvirArjel. Fore xample only `[HttpDelete]` like `get` request. – realist Nov 27 '18 at 11:10
  • No! You cannot Because you are rewriting route parameter name as `studentId`, whereas in the generic configuration it may be only `id`. If you really want to remove this than your method should be `DeleteStudent(long id)` depending on generic configuration. – TanvirArjel Nov 27 '18 at 11:12
  • Ok. Thanks again @TanvirArjel – realist Nov 27 '18 at 11:13
  • Thanks!!! Help me alot!!!! I have been trying a long time to find an answer for my problem, thank you so much!!! – simi Dec 15 '20 at 10:46