Note that there are some explanatory texts on larger screens.

plurals
  1. POWebAPI Controller is not being reached on DELETE command
    text
    copied!<p>I am having difficulty getting the DELETE Method on my Controller to fire when submitting the request over ASP.NET Web API. It returns a 404 but I cannot figure out why. The GET &amp; POST requests work as expected, returning both a list of items as well as a single item when provided an id, but when I call the API using a DELETE request I get a 404 ERROR. </p> <p>Scenario:</p> <h2>1. ASP.NET Web Forms application ...</h2> <p>Not an MVC application although I have installed the MVC4 package in order to leverage the Web API features.</p> <h2>2. Route Table definition in global.asax</h2> <pre><code> RouteTable.Routes.MapHttpRoute( "Default", "api/{controller}/{id}", new { id = RouteParameter.Optional } ); </code></pre> <h2>3. Controller definition</h2> <pre><code> public HttpResponseMessage&lt;Customer&gt; Post(Customer customer) { CustomerDb.Customers.AddObject(customer); CustomerDb.SaveChanges(); var response = new HttpResponseMessage&lt;Customer&gt;(customer, HttpStatusCode.Created); response.Headers.Location = new Uri(Request.RequestUri, "/api/Customer/"+customer.id.ToString()); return response; } public CustomerDTO Get(int id) { CustomerDTO custDTO = null; Customer cust = CustomerDb.Customers.Where(c =&gt; c.id == id).SingleOrDefault(); if (cust == null) throw new HttpResponseException(HttpStatusCode.BadRequest); else custDTO = new CustomerDTO(cust); return custDTO; } public IEnumerable&lt;CustomerDTO&gt; Get() { IQueryable&lt;Customer&gt; custs = CustomerDb.Customers.AsQueryable(); List&lt;CustomerDTO&gt; dto = new List&lt;CustomerDTO&gt;(); foreach (Customer cust in custs) { dto.Add(new CustomerDTO(cust)); } return dto; } public Customer Delete(int id) { Customer cust = CustomerDb.Customers.Where(c =&gt; c.id == id).SingleOrDefault(); if (cust == null) throw new HttpResponseException(HttpStatusCode.BadRequest); CustomerDb.Customers.DeleteObject(cust); CustomerDb.SaveChanges(); return (cust); } </code></pre> <p>I have some of the methods throwing a BadRequest error instead of a 404 when a customer cannot be found so I don't get these responses confused with the REAL problem. Obviously in a real implementation a no customer would return a 404 error. </p> <h2>4. Ajax Call via JQuery to delete item.</h2> <pre><code>function deleteCustomer(id) { var apiUrl = "/api/customer/{0}"; apiUrl = apiUrl.replace("{0}", id); $.ajax({ url: apiUrl, type: 'DELETE', cache: false, statusCode: { 200: function (data) { }, // Successful DELETE 404: function (data) { alert(apiUrl + " ... Not Found"); }, // 404 Not Found 400: function (data) { alert("Bad Request O"); } // 400 Bad Request } // statusCode }); // ajax call }; </code></pre> <p>SO I am expecting that the singel route map should accomodate ALL the scenarios ... </p> <ol> <li>GET api/customer -- Returns ALL customers</li> <li>GET api/customer/5 -- Returns the customer whose ID = 5</li> <li>POST api/customer -- Creates a new customer record</li> <li>DELETE api/customer/5 -- Deletes the customer whose ID = 5</li> </ol> <p>1,2 &amp; 3 work without a problem, just the DELET does not work. I have tried MANY iterations and different tweaks, to no avail. I still feel however that I am overlooking something small. I feel like the problem must be around theRoute mapping but I don't see why this route would not succesfully handle the DELETE request. </p> <p>Any help would be greatly appreciated. </p> <p>Thank You!</p> <p>Gary </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload