首页 > 解决方案 > 对 ASP.NET Core ApiController 的删除请求不起作用

问题描述

我有 apiController 如下所示。而且,我正在delete通过邮递员发送请求。但是,我的删除请求无法访问方法。但是,get方法工作得很好。这个错误的原因是什么?

我的邮递员网址是: http://localhost:5004/api/Student/DeleteStudent/23

[ApiController]
[Route("api/[controller]/[action]")]
public class StudentController : ControllerBase
{
    [HttpDelete("DeleteStudent/{studentId}")]
    public async Task<ServiceResult> DeleteStudent(long studentId)
    {
      return await studentService.DeleteStudent(studentId);
    }

    [HttpGet]
    public async Task<ServiceResult> GetStudents(int studentType)
    {
        return await studentService.GetStudents(studentType);
    }
}

标签: c#angulartypescriptpostman

解决方案


使用[HttpDelete("{studentId}")]代替[HttpDelete("DeleteStudent/{studentId}")]onDeleteStudent()方法如下:

[HttpDelete("{studentId}")]
public async Task<ServiceResult> DeleteStudent(long studentId)
{
  return await studentService.DeleteStudent(studentId);
}

我已经在一个测试项目中对其进行了测试,Postman并且效果很好!


推荐阅读