gpt4 book ai didi

c# - ASP.NET CORE Web API - 删除操作未触发(404 未找到)

转载 作者:行者123 更新时间:2023-11-30 13:54:16 24 4
gpt4 key购买 nike

我有一个简单的 CRUD 操作 Controller 。所有操作都有效,最后一个名为 Delete 的操作是 HTTP DELETE 操作。当我尝试使用示例 url 调用删除操作时:

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

然后应用程序返回 404 Not Found 并且没有触发操作。

在我的其他 Controller 中,删除操作正常工作。一个区别是,在其他 Controller 中,我在 Controller 上有一个路由属性,而不是在每个操作上。这是一个问题?

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

最佳答案

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

您正在混合路线。

重构类如下。

将公共(public)路由作为路由前缀添加到 Controller 中,并利用路由约束

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

关于c# - ASP.NET CORE Web API - 删除操作未触发(404 未找到),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44218133/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com