无法调用Delete Web Api

时间:2013-04-02 17:10:40

标签: asp.net-mvc asp.net-mvc-3 asp.net-web-api

当我尝试调用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>

是否有其他人知道问题可能是什么?我也在我的日志中生成了此事件,表示&#34; 路径&#39;删除&#39;被禁止。&#34;:

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.

at System.Web.HttpMethodNotAllowedHandler.ProcessRequest(HttpContext context)        在System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()        在System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean&amp; completedSynchronously)

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)

1 个答案:

答案 0 :(得分:0)

您正在开发哪种Web服务器?我相信除了get和post之外,旧的cassini开发服务器对http动词没有很大的支持。如果您使用的是cassini,请尝试使用IIS Express。