Asp.net-Web-Api

ASP.NET Web API - 刪除時出現 404

  • July 12, 2015

我正在嘗試Delete在 Web API 控制器上實現一種方法。但是,我總是得到一個404 - Not Found. 在這一點上,我的 GET、POST 和PUT方法工作得很好。我一直在閱讀其他一些關於同一問題的 SO 文章——只是沒有一個有效。

控制器動作

public virtual HttpResponseMessage Delete(string customerId)
{
   adapter.RemoveCustomer(customerId);
   return Request.CreateResponse(HttpStatusCode.OK, "The customer was deleted.");
}

AJAX 請求

function remove(customer, success, error) {
   var url = '/api/Customer';
   var data = JSON.stringify({ 'customerId': customer.CustomerId });
   $.ajax({
       url: url,
       type: 'DELETE',
       data: data,
       contentType: 'application/json'
   })
   .done(function (data, textStatus, handler) {
       success(data);
   })
   .fail(function (handler, textStatus, errorThrown) {
       error(errorThrown);
   });
};

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>

我正在使用 IIS Express,但如果我切換回 Visual Studio Development Server,問題仍然存在。

原始 HTTP

這是 Fiddler 擷取的原始 HTTP 請求:

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)

參數是一個簡單的類型,是從 URI 綁定的,而不是從請求體綁定的。要麼像這樣在查詢字元串中傳遞客戶 ID,要麼將http://localhost:63654/TestMvcApplication/api/Customer?customerId=123簽名更改為public virtual HttpResponseMessage Delete(string id)並使用 URI http://localhost:63654/TestMvcApplication/api/Customer/123

引用自:https://stackoverflow.com/questions/17426942