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

北城以北 提交于 2021-02-07 14:20:31

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/44218133/asp-net-core-web-api-delete-action-not-firing-404-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!