DELETE method .NET WebAPI does not work

落爺英雄遲暮 提交于 2019-12-01 11:45:43

The Solution is to add PUT & DELETE verb in correct applicationHost.config file.

For IIS :

Go to lcation: C:\Windows\System32\inetsrv\config and edit the applicationHost.config file as:

 <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
  <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />  

For IIS EXPRESS GO TO : %userprofile%\documents\iisexpress\config\ and do the same in applicationHost.config file.

Are you hosting under "Default Web Site" ? If yes then try hosting it separately on a different port.

I have the same issue with PUT and DELETE and the workaround was successful.

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