Asp.net web api ASP.NET Web API-删除时为404

Asp.net web api ASP.NET Web API-删除时为404,asp.net-web-api,http-status-code-404,http-method,http-delete,Asp.net Web Api,Http Status Code 404,Http Method,Http Delete,我试图在Web API控制器上实现Delete方法。但是,我总是得到一个404-未找到。在这一点上,我已经得到了GET、POST和PUT方法,这些方法工作得很好。我已经阅读了其他一些关于同一问题的SO帖子,只是没有一篇有效 控制器动作 AJAX请求 Web.Config 这是我的web.config文件。除了模块部分,所有内容都与我创建项目时相同: <system.webServer> <validation validateIntegratedModeConfigur

我试图在Web API控制器上实现
Delete
方法。但是,我总是得到一个
404-未找到
。在这一点上,我已经得到了GET、POST和PUT方法,这些方法工作得很好。我已经阅读了其他一些关于同一问题的SO帖子,只是没有一篇有效

控制器动作 AJAX请求 Web.Config 这是我的
web.config
文件。除了模块部分,所有内容都与我创建项目时相同:

<system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <!--<modules runAllManagedModulesForAllRequests="true"></modules>-->
    <modules>
      <remove name="UrlRoutingModule-4.0" />
      <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
    </modules>
    <handlers>
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <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" />
    </handlers>
</system.webServer>
以下是回应:

HTTP/1.1 404 Not Found
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcdHBhcmtzXERvY3VtZW50c1xHaXRIdWJcVGVzdE12Y0FwcGxpY2F0aW9uXFRlc3RNdmNBcHBsaWNhdGlvblxhcGlcQ3VzdG9tZXI=?=
X-Powered-By: ASP.NET
Date: Tue, 02 Jul 2013 13:11:52 GMT
Content-Length: 220

{"Message":"No HTTP resource was found that matches the request URI 'http://localhost:63654/TestMvcApplication/api/Customer'.","MessageDetail":"No action was found on the controller 'Customer' that matches the request."}
这是一个教我自己的开源软件。我查了最新的资料,以防有人想看到完整的资料来源

public virtual HttpResponseMessage Delete(string customerId)

参数是一个简单类型,它是从URI绑定的,而不是从请求体绑定的。在查询字符串中传递客户ID,如下-
http://localhost:63654/TestMvcApplication/api/Customer?customerId=123
或将签名更改为
公共虚拟HttpResponseMessage Delete(字符串id)
并使用URI
http://localhost:63654/TestMvcApplication/api/Customer/123

此外,HTTP规范指出,与GET相同,带有DELETE请求的有效负载没有语义,因此您永远不应该使用正文发送带有DELETE的参数。我从来没有想到{id}意味着参数名必须是
id
@DarrelMiller谢谢你的提示@Darrel Miller我几乎想过建议OP使用复杂类型,但不确定这是否正确。你的评论真的很有帮助。谢谢
DELETE http://localhost:63654/TestMvcApplication/api/Customer HTTP/1.1
Host: localhost:63654
Connection: keep-alive
Content-Length: 49
Accept: application/json, text/javascript, */*; q=0.01
Origin: http://localhost:63654
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36
Content-Type: application/json
Referer: http://localhost:63654/TestMvcApplication/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8

{"customerId":"e107e2dc20834545ae209849bff195f0"}
HTTP/1.1 404 Not Found
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcdHBhcmtzXERvY3VtZW50c1xHaXRIdWJcVGVzdE12Y0FwcGxpY2F0aW9uXFRlc3RNdmNBcHBsaWNhdGlvblxhcGlcQ3VzdG9tZXI=?=
X-Powered-By: ASP.NET
Date: Tue, 02 Jul 2013 13:11:52 GMT
Content-Length: 220

{"Message":"No HTTP resource was found that matches the request URI 'http://localhost:63654/TestMvcApplication/api/Customer'.","MessageDetail":"No action was found on the controller 'Customer' that matches the request."}
public virtual HttpResponseMessage Delete(string customerId)