WEB API 2 Delete returns 405

我是研究僧i 提交于 2019-12-02 01:07:12

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();
}

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!