【问题标题】:Put, Delete Method is not wokring. error : HTTP/1.1 405 Method Not Allowed放,删除方法不起作用。错误:HTTP/1.1 405 方法不允许
【发布时间】:2021-12-09 16:54:07
【问题描述】:

Get、Post 方法正在运行,但我要运行 Put 和 Delete 请求,然后我会遇到错误消息。

完成项目网址:https://github.com/Dushyantsingh-ds/dotnet-issues/blob/main/Projects/EmployeeService/Readme.md

【问题讨论】:

标签: 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来代替。

    【讨论】:

      猜你喜欢
      • 2014-06-21
      • 2013-03-15
      • 2015-06-20
      • 2017-10-16
      • 1970-01-01
      • 2011-06-21
      • 1970-01-01
      • 1970-01-01
      • 2018-01-25
      相关资源
      最近更新 更多