Guruprasad J Rao
Guruprasad J Rao

Reputation: 29683

type of ajax calls for [HttpDelete] Asp.Net MVC

What type of ajax calls will [HttpDelete] MVC attribute listens to? Say I have below JsonResult decorated with [HttpDelete] attribute.

[HttpDelete]
public JsonResult DeleteData(string id)
{
   //deleting the data
   return new Json{Data="Deleted", JsonRequestBehavior=JsonRequestBehavior.AllowGet};
}

Now I've tried to invoke these JsonResult with $.ajax and $.post but both of them couldn't trace this controller method. Its obvious that I can get this done through [HttpPost] attribute decoration, but since I am deleting a data and this way it should be better IMHO. How would we go invoking Controller methods decorated with [HttpDelete] attribute through ajax?

Upvotes: 0

Views: 2572

Answers (1)

Rion Williams
Rion Williams

Reputation: 76597

MVC will handle mapping the appropriate HTTP Verbs to their corresponding attributes, so that it knows which actions to target (i.e. GET maps to [HttpGet], POST maps to [HttpPost], etc.)

So if you have a Controller Action that is decorated with the [HttpDelete] attribute, you'll need to make a DELETE request to actually hit it.

This can be handled quite easily when making a jQuery AJAX call, as you'll just need to explicitly use type: 'DELETE' as seen below :

$.ajax({
    url: '{your-controller-action}',
    type: 'DELETE',
    success: function(result) {
          // Do something
    }
});

Upvotes: 4

Related Questions