5

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();
  }
}
2
  • 3
    Just curious, did you try changing the delete route to: Route("api/groups/{groupId}/[controller]/{id}")? Commented May 27, 2017 at 14:41
  • @SivaGopal oo this works but I dont understand why in this case [HttpDelete("{id}")] is ignored Commented May 27, 2017 at 14:45

1 Answer 1

9

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();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.