简体   繁体   中英

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.

Complete Project Url : 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

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

you can use https//localhost:44350/api/employee/get for 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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