ASP.NET Web API HTTP删除405方法不允许

use*_*115 10 asp.net-web-api

我已经使用过Web API,并且遇到了很多问题,例如发布多个参数.我升级到WebApi2以使用路由属性,现在遇到如下问题:

"message":"The requested resource does not support http method 'DELETE'."

我花了一整天时间搜索Stack Overflow和Web来解决问题:

  • 删除 webdav
  • 在http protocol允许所有get,put,post,delete
  • 添加了[HTTPDelete]属性
  • 添加 name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0"
  • 谷歌搜索的必要的帮助这里

有人可以指导我吗?

Vin*_*oth 21

我有同样的问题.将以下代码添加到web.config应该可以解决以下问题system.webserver:

<system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <modules runAllManagedModulesForAllRequests="true">
        <remove name="WebDAVModule"/>
Run Code Online (Sandbox Code Playgroud)

  • - > didi it for me:<remove name ="WebDAVModule"/> (2认同)

小智 10

我有同样的问题,因为我的控制器是这样的:

[HttpDelete]
public HttpResponseMessage Delete(string Id)
{
     ....
}
Run Code Online (Sandbox Code Playgroud)

在客户端我使用发送ID作为请求正文:

var url = '/api/upload';
var data = JSON.stringify({ 'Id': id }); // <-- In the body
$.ajax({
    url: url,
    type: 'DELETE',
    data: data, // <-- in the body
    contentType: 'application/json'
})
Run Code Online (Sandbox Code Playgroud)

当我更改客户端以将其用作URL参数时,它工作:

var url = '/api/upload/' + id; // <- In the URL
$.ajax({
    url: url,
    type: 'DELETE',
    contentType: 'application/json'
});
Run Code Online (Sandbox Code Playgroud)