DELETE method .NET WebAPI does not work

后端 未结 3 2084
一整个雨季
一整个雨季 2021-01-15 06:15

I\'ve seen tons of posts about this, but the DELETE method of my new WebAPI simply does not work and returns a 404, using Windows 7 32-bit, IIS 7.5.

I\'ve tried

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-15 07:18

    If You are using attribute routing, update AcceptVerbs with DELETE and PUT methods like this :

    [Route("{fileId:int}")]
    [AcceptVerbs("DELETE")]
    public void Delete(int fileId)
    {
       Files.DeleteFile(fileId);
    }
    

    Also if you are writing fileId as parameter name then dont forget to update the webAPI route config to api/files/{fileId}

    And also use AcceptVerbs attribute for GET and PUT as well because these three have same URL structure to call their methods. Also update the Controller with RoutePrefix as below:

    [RoutePrefix("api/files")]
    public class FileController : ApiController
    {
    }
    

提交回复
热议问题