16

I have an ASP.NET MVC project and I have a single action that accepts GET, POST, and DELETE requests. Each type of request is filtered via attributes on my controllers Action methods.

[ActionName(Constants.AdministrationGraphDashboardAction),
AcceptVerbs(HttpVerbs.Post)]
public ActionResult GraphAdd([ModelBinder(typeof (GraphDescriptorBinder))] GraphDescriptor details);

[ActionName(Constants.AdministrationGraphDashboardAction),
AcceptVerbs(HttpVerbs.Delete)]
public ActionResult GraphDelete([ModelBinder(typeof (RdfUriBinder))] RdfUri graphUri)

I have my GraphAdd method working very well. What I'm trying to figure out is how I can create an HTML <form /> or <a /> (link) that will cause the browser to perform an HTTP Delete request and trigger my GraphDelete method.

If there is a way to do this can someone post some sample HTML and if available the MVC HtmlHelper method I should be using?

Eric Schoonover
  • 47,184
  • 49
  • 157
  • 202

2 Answers2

13

i don't believe this is possible. the method attribute of form elements in HTML4 & XHTML 1.0 will only accept GET or POST. in addition, standard configs of most webservers will deny DELETE and PUT requests. assuming you have configured your webserver to allow methods like PUT / DELETE (such as WebDav does), you could then create your own HTTP request:

DELETE /resource.html HTTP/1.1
Host: domain.com

and handle it appropriately. however, there's no way to do this via a current HTML form. for interest's sake, there is some discussion for DELETE support in HTML5.

Owen
  • 82,995
  • 21
  • 120
  • 115
  • I hope that HTML5 increases support for HTTP verbs. – mshafrir Feb 02 '09 at 20:33
  • For Asp Mvc, there is such a thing as the HttpMethodOverride http://msdn.microsoft.com/en-us/library/ee407388(v=vs.118).aspx. This works by having the browser submit a form POST with a hidden field that overrides the method used when determining which controller action to invoke within the .Net Routing Engine. – Jeremy Bell Mar 05 '14 at 20:18
7

Using XMLHttpRequest, it's not only the "best practice", it's really the only way.