0

I am trying to use ajax to send a delete request to ASP.NET MVC 5 framework. There is a single page with a single red button.

In the CustomersController :

[HttpDelete]
[Route("^/customers/delete/{id:int}")]
public ActionResult Delete(int id)
{
   CarDealerContext ctx = new CarDealerContext();
   // Delete customer with given id...
   // If I get a get a breakpoint in here I will be a happy camper!
   return View();
}

In the RouteConfig :

  routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  routes.MapMvcAttributeRoutes();

In the view I only have :

<input id="deleteBtn" class="btn btn-danger" type="button" 
    value="Delete" data-id="1"/>

This is the script i use when the deleteBtn is clicked :

// The script is loaded after Bootstrap and jQuery.

 $("#deleteBtn").on("click", function (event) {
     event.preventDefault();
     var id = $("#deleteBtn").attr("data-id");
     $.ajax({
        url: "/customers/delete/" + id,
        type: 'delete',
        data: { id: id }
     });
 })
 // The request sends http://localhost:61402/customers/delete/1
 // and the response is 404...

All I want is to activate the Delete method in the CustomersController. I have tried pretty much everything in the Route(regex) attribute. The second I change the Method to [HttpGet] it works. Would also appreciate a good studying source on the subject.

2 Answers 2

3

You need to enable these verbs in IIS (put, delete)

Iisexpress may need config file edit

Sign up to request clarification or add additional context in comments.

3 Comments

Would have never thought of that.. Thanks for the information !
I did, but I don't have enough reputation for it to count... sadly. I would upvote twice.
No problems, nice one.
0

Change your Action Method as follows:

[HttpGet]
public ActionResult Delete(int id)
{
    //Your Code
}

Two points to note:

1) Instead of HTTPDelete, you should set an HTTPGet annotation because the request you're sending from your view is a GET request, not one that's conventionally understood as a delete operation type request.

2) This may not be required in your case but I thought I'd mention it anyhow. ASP.NET MVC prefers convention over configuration. You don't need to explicitly set that route. The default route in the RouteConfig file will point to your method.

This would be the default configuration in the RouteConfig file:

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = request to this Action Method (if you don't have it, it's probably a good idea UrlParameter.Optional }
            );

4 Comments

All i need to change is [HttpDelete] to [HttpGet]. I know that works, but what is the point of having a [HttpDelete] attribute ? Is it common practice to use only POST and GET ?
Yes, it is. Generaly HttpGet is used when exposing APIs. If you're the one managing the access points to that method (i.e. from your view), then you should stick with GET and POST. If you were developing an API and wanted to make it explicitly clear to anyone invoking your method that this call would imply a delete operation (since the action name could be anything), then you could use HttpDelete.
You can also set the type of your AJAX call to DELETE as well (i.e. by setting type: 'DELETE' but I'd suggest sticking to GET and POST.
Any time you allow a GET request to modify data, you’re asking for trouble - Phil Haack haacked.com/archive/2009/04/02/anatomy-of-csrf-attack.aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.