WEB API 2 Delete returns 405

后端 未结 4 650
深忆病人
深忆病人 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();
    }
    

提交回复
热议问题