【问题标题】:Put, Delete Method is not wokring. error : HTTP/1.1 405 Method Not Allowed放,删除方法不起作用。错误:HTTP/1.1 405 方法不允许
【发布时间】:2021-12-09 16:54:07
【问题描述】:
【问题讨论】:
标签:
c#
asp.net
.net
webapi
【解决方案1】:
您的删除端点还应该有一个[Route(...)] 数据注释:
[Route("api/employee/{EmpId}")]
【解决方案2】:
您必须决定要使用什么 - 配置文件中的属性路由或默认路由。
目前最常用的 API 使用方式是将属性路由分配给控制器
[Route("~/api/[controller]/[action]]
public class EmployeeController : ApiController
您可以使用 https//localhost:44350/api/employee/get 来获取 Get()
等等
// /api/employee/get
public IEnumerable<Employee> Get()
// /api/employee/get/5
[HttpGet("{empId}")]
public HttpResponseMessage Get(int empId)
// /api/employee/post" for
public HttpResponseMessage Post([FromBody] Employee employee)
// /api/employee/delete/5
[Route("{empId}")]
public HttpResponseMessage Delete(int empId)
// /api/employee/put/5
[Route("{empId}")]
public HttpResponseMessage Put(int empId, [FromBody] Employee employee)
而且由于你不把方法作为动作属性,所以你不需要使用delete和put,你可以使用get和post来代替。