ASP.NET CORE Web API-无法触发删除操作(找不到404) [英] ASP.NET CORE Web API - delete action not firing (404 not found)

查看:74
本文介绍了ASP.NET CORE Web API-无法触发删除操作(找不到404)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CRUD操作的简单控制器.所有操作均正常运行,最后一个名为Delete的操作是HTTP DELETE操作.当我尝试使用示例网址调用删除操作时:

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

然后,应用程序返回404 Not Found(未找到),并且操作未触发.

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

推荐答案

我不明白为什么在这种情况下[HttpDelete("{id}")]被忽略.

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

您正在混合路线.

按如下所示重构类.

将通用路由添加为控制器的路由前缀,并利用路由约束

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

这篇关于ASP.NET CORE Web API-无法触发删除操作(找不到404)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆