ASP.NET CORE Web API - 删除操作未触发(未找到 404)
ASP.NET CORE Web API - delete action not firing (404 not found)
我有一个简单的 CRUD 操作控制器。所有操作都有效,最后一个名为 Delete 的操作是 HTTP DELETE 操作。当我尝试使用示例 url 调用删除操作时:
然后应用程序 returns 404 未找到并且操作未触发。
在我的其他控制器中,删除操作工作正常。一个区别是,在其他控制器中,我在控制器上有一个路由属性,而不是在每个操作上。这是个问题?
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.
您正在混合路线。
重构 class 如下。
将公共路由添加到控制器作为路由前缀,同时利用路由约束
[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();
}
}
我有一个简单的 CRUD 操作控制器。所有操作都有效,最后一个名为 Delete 的操作是 HTTP DELETE 操作。当我尝试使用示例 url 调用删除操作时:
然后应用程序 returns 404 未找到并且操作未触发。
在我的其他控制器中,删除操作工作正常。一个区别是,在其他控制器中,我在控制器上有一个路由属性,而不是在每个操作上。这是个问题?
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.
您正在混合路线。
重构 class 如下。
将公共路由添加到控制器作为路由前缀,同时利用路由约束
[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();
}
}