简体   繁体   English

放置,删除方法不起作用。 错误:HTTP/1.1 405 方法不允许

[英]Put, Delete Method is not wokring. error : HTTP/1.1 405 Method Not Allowed

Get, Post methods are working, but I'm going to run the Put and Delete request then I face an error message. Get、Post 方法正在运行,但我要运行 Put 和 Delete 请求,然后我会遇到错误消息。

Complete Project Url : https://github.com/Dushyantsingh-ds/dotnet-issues/blob/main/Projects/EmployeeService/Readme.md完整的项目网址: https : //github.com/Dushyantsingh-ds/dotnet-issues/blob/main/Projects/EmployeeService/Readme.md

在此处输入图片说明

您的删除端点还应该有一个[Route(...)]数据注释:

[Route("api/employee/{EmpId}")]

you have to decide what are you going to use - attribute routing or default routing from config file.您必须决定要使用什么 - 配置文件中的属性路由或默认路由。

For today the most common way to use API is to assign attribute routing to the controller目前最常用的 API 使用方式是为控制器分配属性路由

[Route("~/api/[controller]/[action]]
public class EmployeeController : ApiController

you can use https//localhost:44350/api/employee/get for Get()你可以使用 https//localhost:44350/api/employee/get 来获取 Get()

and so on等等

 // /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)
      

and since you don't put methods as action attributes , you don't need to use delete and put, you can use get and post instead.并且由于您没有将方法作为操作属性,因此您不需要使用 delete 和 put,您可以使用 get 和 post 代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM