Search code examples
c#restful-architecturenancy

What is the best practice to call a DELETE restful method from a web page?


I am building a small Nancy webapp which would do CRUD operations on a table. It uses GET, POST and DELETE verbs. I want to have a link in the web page to call the delete method. Using a "anchor" tag would use the GET method, by default. I am not comfortable with changing the webservice to use GET verb to do delete operations as it would break RESTful concept. What is the best practice in such situation?


Solution

  • You should not use the GET verb for desctructive/state changing operations so using DELETE or POST is the way to go (DELETE is semantically correct in your case).

    With javascript you can hook on the anchor click event and fire the request with the DELETE verb.

    A small sample with Jquery's ajax method:

    <a href='/deleteAction/myId' class='deleteLink'>Delete me</a>
    
    $('.deleteLink').click(function(e){
       e.preventDefault();
       $.ajax({
         url: $(this).attr('href'),
         type: 'DELETE',
         success: function(result) {        
      }});
    });