删除对ASP.NET Core ApiController的请求不起作用

时间:2018-11-27 10:39:58

标签: c# angular typescript postman

我有如下所示的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);
    }
}

1 个答案:

答案 0 :(得分:2)

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

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

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