Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to implement a Delete method on a Web API controller. However, I always get a 404 - Not Found. At this point, I have GET, POST and PUT methods that are working just fine. I've been reading a handful of the other SO posts about the same issue - just none of them are working.

The Controller Action

public virtual HttpResponseMessage Delete(string customerId)
{
    adapter.RemoveCustomer(customerId);
    return Request.CreateResponse(HttpStatusCode.OK, "The customer was deleted.");
}

The AJAX Request

function remove(customer, success, error) {
    var url = '/api/Customer';
    var data = JSON.stringify({ 'customerId': customer.CustomerId });
    $.ajax({
        url: url,
        type: 'DELETE',
        data: data,
        contentType: 'application/json'
    })
    .done(function (data, textStatus, handler) {
        success(data);
    })
    .fail(function (handler, textStatus, errorThrown) {
        error(errorThrown);
    });
};

The Web.Config

This is my web.config file. Except for the modules section, everything is the same as when I created the project:

<system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <!--<modules runAllManagedModulesForAllRequests="true"></modules>-->
    <modules>
      <remove name="UrlRoutingModule-4.0" />
      <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
    </modules>
    <handlers>
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
      <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>

I am using IIS Express, but the issue still occurs if I switch back to Visual Studio Development Server.

The Raw HTTP

Here is the raw HTTP request captured by Fiddler:

DELETE http://localhost:63654/TestMvcApplication/api/Customer HTTP/1.1
Host: localhost:63654
Connection: keep-alive
Content-Length: 49
Accept: application/json, text/javascript, */*; q=0.01
Origin: http://localhost:63654
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36
Content-Type: application/json
Referer: http://localhost:63654/TestMvcApplication/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8

{"customerId":"e107e2dc20834545ae209849bff195f0"}

And here is the response:

HTTP/1.1 404 Not Found
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcdHBhcmtzXERvY3VtZW50c1xHaXRIdWJcVGVzdE12Y0FwcGxpY2F0aW9uXFRlc3RNdmNBcHBsaWNhdGlvblxhcGlcQ3VzdG9tZXI=?=
X-Powered-By: ASP.NET
Date: Tue, 02 Jul 2013 13:11:52 GMT
Content-Length: 220

{"Message":"No HTTP resource was found that matches the request URI 'http://localhost:63654/TestMvcApplication/api/Customer'.","MessageDetail":"No action was found on the controller 'Customer' that matches the request."}

This is an open source project for teaching myself. I have checked in the latest in case anyone wants to see the complete source.

share|improve this question

1 Answer

up vote 5 down vote accepted
public virtual HttpResponseMessage Delete(string customerId)

Parameter is a simple type and is bound from URI and not from request body. Either pass the customer ID in query string like this - http://localhost:63654/TestMvcApplication/api/Customer?customerId=123 or change the signature to public virtual HttpResponseMessage Delete(string id) and use a URI http://localhost:63654/TestMvcApplication/api/Customer/123.

share|improve this answer
4  
Also, the HTTP spec states that payloads with DELETE requests have no semantics, the same as GET, so you should never use the body to send a parameter with a delete. tools.ietf.org/html/draft-ietf-httpbis-p2-semantics-22#page-28 –  Darrel Miller Jul 2 at 13:46
1  
It never occurred to me that {id} meant that parameter name had to be id. –  Travis Parks Jul 2 at 13:46
 
@DarrelMiller Thanks for the tip! –  Travis Parks Jul 2 at 13:47
 
@Darrel Miller I almost thought of suggesting OP to use a complex type but was not sure if that is right or not. Your comment is really helpful. Thanks. –  Badri Jul 2 at 13:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.