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 開発サーバーに戻すと問題が発生します。
生の 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."}
これは、独学のためのオープン ソースプロジェクトです。誰かが完全なソースを見たい場合に備えて、最新のものをチェックインしました.