Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET CORE Web API - delete action not firing (404 not found)

I have a simply controller to CRUD operations. All actions works expect the last action named Delete which is HTTP DELETE action. When I try call delete action with example url:

http://localhost/api/groups/1/attendances/10

then application returns 404 Not Found and action is not firing.

In my other controllers delete action works correctly. One difference is that in others controllers I have one route attribute on controller instead of on each action. This is a problem?

public class AttendancesController : Controller
{
  public AttendancesController(IGroupService groupService, IAttendanceService attendanceService, IPersonService personService, IPersonAttendanceService personAttendanceService)
  {
     //
  }

  [Route("api/groups/{groupId}/[controller]")]
  [HttpGet]
  public IActionResult GetAttendancesForGroup(int groupId)
  {
     //
  }

  [Route("api/groups/{groupId}/[controller]/{date}")]
  [HttpGet]
  public IActionResult GetAttendanceForGroup(int groupId,  DateTime date)
  {
     //
  }

  [Route("api/groups/{groupId}/[controller]")]
  [HttpPost]
  public IActionResult CreateAttendanceForGroup(int groupId, [FromBody] AttendanceCreateDto dto)
  {
     //
  }


  [Route("api/people/{personId}/[controller]")]
  [HttpGet]
  public IActionResult GetAttendancesForPerson(int personId)
  {
    //
  }

  [Route("api/groups/{groupId}/[controller]")]
  [HttpDelete("{id}")]
  public IActionResult Delete(int groupId, int id)
  {
     var group = _groupService.FindById(groupId);
     if (group == null)
        return NotFound();
     var attendance = _attendanceService.GetAttendanceByIdAndGroupId(id,groupId);
     if (attendance == null)
        return NotFound();

     _attendanceService.Delete(attendance);
     return NoContent();
  }
}
like image 296
user140503 Avatar asked May 27 '17 14:05

user140503


1 Answers

I dont understand why in this case [HttpDelete("{id}")] is ignored.

You are mixing routes.

Refactor the class as follows.

Add the common route to the controller as a route prefix and also take advantage to route constraints

[Route("api/groups/{groupId}/[controller]")]
public class AttendancesController : Controller {
    public AttendancesController(IGroupService groupService, IAttendanceService attendanceService, IPersonService personService, IPersonAttendanceService personAttendanceService) {
        //
    }


    [HttpGet] // Matches GET api/groups/1/attendances
    public IActionResult GetAttendancesForGroup(int groupId) {
        //
    }


    [HttpGet("{date:datetime}")] //Matches GET api/groups/1/attendances/2017-05-27
    public IActionResult GetAttendanceForGroup(int groupId,  DateTime date) {
        //
    }

    [HttpPost] // Matches POST api/groups/1/attendances
    public IActionResult CreateAttendanceForGroup(int groupId, [FromBody] AttendanceCreateDto dto) {
        //
    }


    [HttpGet("~/api/people/{personId}/[controller]")] // Matches GET api/people/1/attendances
    public IActionResult GetAttendancesForPerson(int personId) {
        //
    }

    [HttpDelete("{id:int}")] // Matches DELETE api/groups/1/attendances/10
    public IActionResult Delete(int groupId, int id) {
        var group = _groupService.FindById(groupId);
        if (group == null)
            return NotFound();
        var attendance = _attendanceService.GetAttendanceByIdAndGroupId(id,groupId);
        if (attendance == null)
            return NotFound();

        _attendanceService.Delete(attendance);
        return NoContent();
    }
}
like image 68
Nkosi Avatar answered Nov 16 '22 12:11

Nkosi