Postman DELETE requests result in 404

I am currently working on the section titled Building RESTful Services with ASP.NET Web API. I
am trying to delete customers, however every attempt that I have made has resulted in a 404 error. I looked at the code on github but could not figure out why the DELETE requests are failing.

This is a copy of my delete method:
[HttpDelete]
public IHttpActionResult DeleteCustomer(int id)
{
var customerInDb = _context.Customers.SingleOrDefault(x => x.Id == id);

        if (customerInDb == null)
            return NotFound();

        _context.Customers.Remove(customerInDb);
        _context.SaveChanges();

        return Ok();
    }

My web Api configuration expects a route of the form: “api/{controller}/{id}”

In result, in order to delete a customer, I send a DELETE request to: https://localhost:44313/api/customers/0

Anyone have any ideas of what could be going on?

Hi,
At first sight it seems correct to me.
Now did you try putting a breakpoint and send a request to make sure the action is getting called?
Regards.

Hello,

Yes, I put a break point on the line where customerInDb was created. When I sent a DELETE request, the break point was never reached. I suspect that the computer does not know to call the delete method inside of the API, but I do not know what else it would try to call.

I noticed that the id of all of my customers was 0 and I found this to be helpful: Vidly Customers API returns customers with id=0

My DELETE requests still do not work, but I just thought that I should mention this.

This is exactly what I expected.
What I asked you to do is to make sure you call your endpoint properly.
This happened to me also.
Check everything such as id, typo, routing configuration.

Check the URL of the request sent from the Browser.
Something makes the URL malformed and thus the endpoint is never called.

PS : How are you calling the endpoint?
In-App? Swagger ? Postman ? Other ?
The problem lies likely there.

I am using the Postman app on Windows. My request looks like this:

Also I have been able to make GET requests without a problem. For some reason it just seem like my computer cannot find the delete method inside of the API.

Does changing the URL to lower case (/api/customers) fix it (in Postman you are using an uppercase C)?

Have you checked in your code that you have set up the endpoint properly (correct endpoint name, method type to delete)?

Thanks, I did try that and unfortunately it did not work. Also, in my recent attempts to fix things I have caused my server to stop working, so I think I am just going to restart.

@sufian.babri, @UniqueNospaceShort Thank you for trying to help me out

On my API it is case insensitive. That said, it’s sure a thing to try.

Can you update your annotation like this.

[HttpDelete("{id}")]

Can you show us your RoutesConfig.cs file please?

This is mine

using System.Web.Mvc;
using System.Web.Routing;

namespace Vidly
{
   public class RouteConfig
   {
      public static void RegisterRoutes(RouteCollection routes)
      {
         routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

         // The important line is this one. Then you can use attribute routing.
         // [Route("/api/Customers/{id}")]
         routes.MapMvcAttributeRoutes();

         //routes.MapRoute("MoviesByReleaseDate", 
         //    "movies/released/{year}/{month}", 
         //    new { controller="Movies", action="ByReleaseDate"}, 
         //    new { year= @"\d{4}", month= @"\d{2}"});

         routes.MapRoute(
             name: "Default",
             url: "{controller}/{action}/{id}",
             defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
         );
      }
   }
}