WEB API 2 Delete returns 405

試著忘記壹切 提交于 2019-12-20 03:09:22

问题


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.

Here is my class:

[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've tried:

  • several different variants of the method delete by using an int as identifier, using IHttpActionResult
  • Changing the web.config
  • Adding a route definition on the Delete method itself

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>

And here is my request:

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

The response is this:

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

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


回答1:


Also wanted to add another answer that someone told me about:

If you have this in your route config:

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

Your id should always be called id and cannot be named something else:

This doesn't work

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

This does

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



回答2:


Your controller is called AccountController and the method is called 'delete' - so don't you need to send in a HTTP DELETE /api/account/delete/JoopSloop in order to match the request to the method.




回答3:


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

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.




回答4:


If somebody is having this problem with only DELETE and PUT in the httprequest. I had it and it was solved by removing WebDav. https://inedo.com/support/kb/1140/disabling-webdav-in-iis



来源:https://stackoverflow.com/questions/27890314/web-api-2-delete-returns-405

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!