Search code examples
c#angularjsasp.net-web-apiasp.net-web-api-routing

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


Solution

  • 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
            })