Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebApi "The requested resource does not support http method 'DELETE"

Using WebApi angularjs project and trying to delete function as `

     [HttpDelete]
    public String DeleteCountry(string cntryId)
    {
        if (cntryId != null)
        {
            return repoCountry.DeleteCountry(Convert.ToInt32(cntryId));

        }
        else
        {
            return "0";

        }
    }

js function is

  $http({
            method: "delete",
            url: '/api/Country/DeleteCountry/',
            dataType: "json",
            data: { cntryId: cntryId }
        }).then(function (response) {});

Here I am getting exception

{"Message":"The requested resource does not support http method 'DELETE'."}

Insertion,update and get functionalities are working correctly.Giv a solution and why it is happening for delete only

like image 707
safeena rasak Avatar asked Jun 14 '17 09:06

safeena rasak


3 Answers

Adorn your method with the Route attribute (I see this give me more control on the routing behavior in web API) and pass your data parameters as constructor args in this format: [HttpDelete, Route("{cntryId}"):

[HttpDelete, Route("{cntryId}")]
public String DeleteCountry(string cntryId)
  {
    //....
  }

in the angular controller, you can just do this:

$http.delete('/api/Country/' + cntryId).then(function (response) {
            //if you're waiting some response
        })
like image 111
mshwf Avatar answered Oct 17 '22 18:10

mshwf


is not a webapi issue is more a the format of your query . the message says that does not support http method 'DELETE' because the webapi delete method is expecting an id as a parameter. and the route has the following format routeTemplate: "api/{controller}/{id}", to resolve your issue try to use fiddler to intercept your request and ensure that your delete request is sent as '/api/Country/DeleteCountry/'+cntryId,

like image 1
BRAHIM Kamel Avatar answered Oct 17 '22 18:10

BRAHIM Kamel


I faced same issue and solved it finally. This is simple and stupid solution.

Earlier code

public String DeleteCountry(string cntryId)

Changed code

public String DeleteCountry(int id)

so just use 'int id' not 'cntryId'. I did not use [HttpDelete] before method name but it worked.

like image 1
Suman Biswas Avatar answered Oct 17 '22 19:10

Suman Biswas