当我尝试调用 Delete Web API 时,我得到:
NetworkError: 404 Not Found - /api/tradespendentryip/5
我的 API 控制器如下所示:
public class TradeSpendEntryIPController : ApiController
{
// DELETE api/tradespendentryip/5
public void Delete(int id)
{
string x = id.ToString(); //I put a breakpoint here which never gets hit
}
}
我用这个 jQuery 片段调用它,硬编码创建 Web API 时生成的提示 URL:
$.ajax({
type: "DELETE",
url: "/api/tradespendentryip/5"
}).done(function( msg ) {
alert( 'Record deleted.');
}).fail(function(jqXHR, textStatus){
alert('Unable to delete this record at this time.');
});
检查 Firebug 中的网络选项卡,我总是得到“NetworkError:404 Not Found - /api/tradespendentryip/5”
根据网络上的一些研究,我尝试调整我的 web.config 以便 WebDAV 不会干扰:
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule" />
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />
<add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" preCondition="managedHandler" />
<add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" preCondition="managedHandler" />
</modules>
<handlers>
<remove name="WebDAV" />
<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>
有没有人知道可能是什么问题?我的日志中也生成了这个事件,表明“路径'DELETE'被禁止。 ”:
Event code: 3005
Event message: An unhandled exception has occurred.
Event time: 02/04/2013 1:50:18 PM
Event time (UTC): 02/04/2013 4:50:18 PM
Event ID: 646d46d6edab4c049b10948279201c2f
Event sequence: 42
Event occurrence: 1
Event detail code: 0
Application information:
Application domain: 972ee575-22-130093949467510162
Trust level: Full
Application Virtual Path: /
Application Path: C:\Sites\TPS\Development\Source\TPS\Source\TPS.Website\
Machine name: MACHINENAME
Process information:
Process ID: 6532
Process name: WebDev.WebServer40.exe
Account name: domain\chardie
Exception information:
Exception type: HttpException
Exception message: Path 'DELETE' is forbidden.
在 System.Web.HttpMethodNotAllowedHandler.ProcessRequest(HttpContext context) 在 System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 在 System.Web.HttpApplication.ExecuteStep(IExecutionStep 步骤,布尔和完成同步)
Request information:
Request URL: /Canada/TradeSpendingSales/api/tradespendentryip/5
Request path: /Canada/TradeSpendingSales/api/tradespendentryip/5
User host address: ::1
User: domain\chardie
Is authenticated: True
Authentication Type: NTLM
Thread account name: domain\chardie
Thread information:
Thread ID: 14
Thread account name: domain\chardie
Is impersonating: False
Stack trace: at System.Web.HttpMethodNotAllowedHandler.ProcessRequest(HttpContext context)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)