不允许使用Web API DELETE请求

时间:2014-08-04 15:53:49

标签: asp.net-mvc angularjs asp.net-web-api iis-6

我的web.config下面。我的本地主机上的一切都很好,但上传到亚马逊服务器(IIS 6.1)后,“DELETE”请求不允许,所有其他动词都按预期工作(包括PUT)。

我的web.config

  ...
     <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>
...

我的角度控制器

function deleteSlide (post)
     {
       post.$delete({ id: post.PostId }, function(){alert("deleted")},
       function(){alert("can't delete")}
    );  
 }

用于删除的web api操作:

[ResponseType(typeof(post))]
        public async Task<IHttpActionResult> DeletePost(int id)
        {
            Post post= await db.Posts.FindAsync(id);
            if (post== null)
            {
                return NotFound();
            }

            db.Post.Remove(post);
            await db.SaveChangesAsync();

            return Ok(post);
        }

2 个答案:

答案 0 :(得分:0)

尝试删除WebDavModule:

<modules runAllManagedModulesForAllRequests="true">
    <remove name="WebDAVModule"/>
</modules>
...
<handlers>
    <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
    <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>

答案 1 :(得分:0)

这种情况请仔细检查post.PostId是否为空,因为其余代码似乎没问题。

function deleteSlide (post)
     {
       console.log("Postid=" + post.PostId) //<- can't be null
       post.$delete({ id: post.PostId }, function(){alert("deleted")},
       function(){alert("can't delete")}
    );  
 }