2

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..

VeldMuijz
  • 605
  • 5
  • 18

4 Answers4

6

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();
}
VeldMuijz
  • 605
  • 5
  • 18
1

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.

Code Uniquely
  • 6,356
  • 4
  • 30
  • 40
  • Sendin it to `/api/account/delete/JoopSloop` gives me an `/api/account/delete/JoopSloop` – VeldMuijz Jan 11 '15 at 20:16
  • pressed enter to quickly: Sending it to `/api/account/delete/JoopSloop` gives me an `http 404`. Setting the first word of the method to the HTTP verb makes it route to that automatically, I believe. And if you specifically add the `[HttpDelete]` attribute to the method it should match the HTTP delete messages coming in to that method. I have a method `GetUsers`, with the `[HttpGet]` attribute and am able to get the users via httpget on /api/Accounts. – VeldMuijz Jan 11 '15 at 20:23
1

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.

VeldMuijz
  • 605
  • 5
  • 18
0

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

MichaelGul
  • 49
  • 6