WEB API 2 Delete返回405 [英] WEB API 2 Delete returns 405

查看:186
本文介绍了WEB API 2 Delete返回405的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的Web API类中创建一个删除功能。
我之前使用Put和Patch Http消息时遇到了问题,因为这些消息被链接到WebDAV。
更改此功能后,Patch和Put工作正常,但删除功能会给我带来问题。

I'm trying to do create a delete function in my web API class. I had problems earlier with using the Put and Patch Http messages since these were being linked to WebDAV. After changing this the Patch and Put worked but the Delete is giving me issues.

这是我的课程:

[RoutePrefix("api/Account")]
public class AccountController : ApiController
{
     //private AuthRepository _repo = null;
    Orchestrate.Net.Orchestrate orchestrate = new Orchestrate.Net.Orchestrate("0b42c04c-0d70-4da8-a3c1-2036882369d0");

[..rest of class here..]

// DELETE: api/account/5
[AllowAnonymous]
[HttpDelete]
public void Delete(string username)
{
   orchestrate.Delete("users", username, true);
}

}

我试过:


  • 使用I作为标识符删除方法的几种不同变体,使用IHttpActionResult

  • 更改网页.config

  • 在Delete方法本身上添加路由定义

浏览网页时我们发现很多人在使用他们的Web.Config文件时遇到了麻烦但我的似乎很好。以下是每个人都在谈论的部分。

When browsing the web i've found that many people have troubles with their Web.Config file but mine seems to be fine. Here is the part everybody is talking about.

  <system.webServer>
    <modules>   <remove name="WebDAVModule"/> </modules>
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <remove name="WebDAV" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>   
   </system.webServer>

这是我的要求:

DELETE http://localhost:41021/api/account/JoopSloop HTTP/1.1
Host: localhost:41021
Connection: keep-alive
Accept: application/json, text/plain, */*
Origin: http://localhost:48898
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36
Authorization: Bearer -AqDRUMrrBNGICNUGIiSn0-gxTBUzElKupPPO9m1bCj0KHA9Z74vnOrPCxU-sTAWlfymTCDD3WGdFETC0-20zXOVSB7aStVHtCFrr-u9zogsUWfdiSicNzZQE3xrbyiFTB71GuwFjchx8xHIFI_6qHB26E2EKITwlFSi7X7p-lo6WWd4Z12SdL02ZxOI1wyZ8MQiXN47X6ZvuDKC6B_rJGQ2qh5p8pA8quZ0p8TvDLrPG6IuXv1U8jjS1iZCTVXO
Referer: http://localhost:48898/
Accept-Encoding: gzip, deflate, sdch
Accept-Language: nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4

答案如下:

{"message":"The requested resource does not support http method 'DELETE'."}

所以我开始非常绝望了..

So I'm starting to get pretty desperate here..

推荐答案

我在代码中添加了以下内容以使其正常工作:

I added the following to my code to make it work:

    // DELETE: api/account/Janjaap/Admin
    [Authorize]
    [HttpDelete]
    [Route("delete/{account}/{user}")]
    public IHttpActionResult DeleteUser(string account, string user){
         //Do delete logic here
         Return Ok();
    }

我还没有真正解决刚用方法修饰方法的初始问题。我尝试使用的初始路由是 Route(/ delete),这也会导致问题。删除第一个 / 是必须的。

I haven't really resolve the initial issue just decorated the method with a route. The initial route that I tried to use was Route("/delete") which will also cause problems. Deleting the first / is a must.

这篇关于WEB API 2 Delete返回405的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆