I\'m trying to do create a delete function in my web API class. I had problems earlier with using the Put and Patch Http messages since these were being linked to WebDAV. After
Also wanted to add another answer that someone told me about:
If you have this in your route config:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Your id should always be called id and cannot be named something else:
This doesn't work
//api/account/useridvalue
[Authorize]
[HttpDelete]
public IHttpActionResult Delete(string whatEverYourNameis){
//Do delete logic here
Return Ok();
}
This does
//api/account/useridvalue
[Authorize]
[HttpDelete]
public IHttpActionResult Delete(string id){
//Do delete logic here
Return Ok();
}
I added the following to my code to make it work:
// DELETE: api/account/Janjaap/Admin
[Authorize]
[HttpDelete]
[Route("delete/{account}/{user}")]
public IHttpActionResult DeleteUser(string account, string user){
//Do delete logic here
Return Ok();
}
I haven't really resolve the initial issue just decorated the method with a route. The initial route that I tried to use was Route("/delete")
which will also cause problems. Deleting the first /
is a must.
If somebody is having this problem with only DELETE and PUT in the httprequest. I had it and it was solved by removing WebDav. https://inedo.com/support/kb/1140/disabling-webdav-in-iis
Your controller is called AccountController and the method is called 'delete' - so don't you need to send in a HTTP DELETE /api/account/delete/JoopSloop in order to match the request to the method.