WEB API 2 Delete返回405

Vel*_*ijz 2 asp.net http http-delete asp.net-web-api2

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

这是我的班级:

[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);
}

}
Run Code Online (Sandbox Code Playgroud)

我试过了:

  • 使用IHttpActionResult,使用int作为标识符删除方法的几个不同变体
  • 更改web.config
  • 在Delete方法本身上添加路由定义

浏览网页时,我发现许多人的Web.Config文件都有问题,但我的网络似乎很好.这是每个人都在谈论的部分.

  <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>
Run Code Online (Sandbox Code Playgroud)

这是我的要求:

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
Run Code Online (Sandbox Code Playgroud)

答案是这样的:

{"message":"The requested resource does not support http method 'DELETE'."}
Run Code Online (Sandbox Code Playgroud)

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

Vel*_*ijz 6

还想添加别人告诉我的另一个答案:

如果你的路线配置中有这个:

config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
Run Code Online (Sandbox Code Playgroud)

您的ID应始终被称为id,并且不能以其他名称命名:

这不起作用

//api/account/useridvalue
[Authorize]
[HttpDelete]
public IHttpActionResult Delete(string whatEverYourNameis){
     //Do delete logic here
     Return Ok();
}
Run Code Online (Sandbox Code Playgroud)

这样做

//api/account/useridvalue
[Authorize]
[HttpDelete]
public IHttpActionResult Delete(string id){
     //Do delete logic here
     Return Ok();
}
Run Code Online (Sandbox Code Playgroud)