WEB API 2 Delete returns 405

后端 未结 4 647
深忆病人
深忆病人 2021-01-21 20:06

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

相关标签:
4条回答
  • 2021-01-21 20:10

    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();
    }
    
    0 讨论(0)
  • 2021-01-21 20:20

    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.

    0 讨论(0)
  • 2021-01-21 20:20

    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

    0 讨论(0)
  • 2021-01-21 20:29

    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.

    0 讨论(0)
提交回复
热议问题