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);
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.
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.
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.
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.
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 }
);
}
}
}
Sorry to take so long to respond, and thanks for sticking with me. I ended up getting rid of the project entirely because there were too many problems with my project. I am now going through the angular4 course and might get back to this some other day. Thanks again though.