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
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
})
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,
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With