ASP.NET CORE Web API - remove action not triggering (404 not found)

I have a simple controller for CRUD operations. All actions work by waiting for the last action named Delete, which is an HTTP DELETE action. When I try to call delete action with example url:

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

then the app returns 404 Not Found and the action doesn't start.

In my other controllers, deleting an action works correctly. One difference is that in other controllers, I have one route attribute on the controller, not on every action. This is 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();
  }
}

      

+3


source to share


1 answer


I don't understand why [HttpDelete ("{id}")] is being ignored in this case.

You are mixing routes.

Reframe the class as follows.



Add a shared route to the controller as a route prefix, and take advantage of route restrictions

[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();
    }
}

      

+4


source







All Articles