ASP.NET Web API上默认不允许HTTP PUT和DELETE

时间:2015-01-23 18:03:50

标签: c# asp.net asp.net-mvc

我无法遵循Microsoft提供的本教程,因为默认情况下,在最新的ASP.NET Web API脚手架上会阻止DELETE和PUT:

http://www.asp.net/web-api/overview/older-versions/creating-a-web-api-that-supports-crud-operations

当我尝试执行PUT或DELETE时,我最终得到了来自服务器的响应:

HTTP/1.1 405 Method Not Allowed
Cache-Control: no-cache
Pragma: no-cache
Allow: GET,POST
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?QzpcVXNlcnNcTWF0dFxEb2N1bWVudHNcV2lubm92IEdpdEh1YiBSZXBvc2l0b3JpZXNcUkVTVGZ1bC1Db21tYW5kLUNlbnRlclxBU1AuTkVULVJFU1RmdWwtQ29tbWFuZC1DZW50ZXItSW50ZXJmYWNlXFJFU1RmdWxDb21tYW5kQ2VudGVySW50ZXJmYWNlXGFwaVxWaWRlb01peFw=?=
X-Powered-By: ASP.NET
Date: Fri, 23 Jan 2015 17:55:05 GMT
Content-Length: 75

{"Message":"The requested resource does not support http method 'DELETE'."}

从这个错误中我得出的唯一结论是,我的网络应用程序在调试模式下根本不允许使用PUT和DELETE,而且我不知道该怎么做。

此问题目前仅在调试模式下发生,我尚未在生产IIS计算机上正式部署此版本。可以做些什么来允许PUT和DELETE?

这是我的web.config文件的样子:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=301879
  -->
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-RESTfulCommandCenterInterface-20141125105128.mdf;Initial Catalog=aspnet-RESTfulCommandCenterInterface-20141125105128;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
  <appSettings></appSettings>
  <system.web>
    <authentication mode="None" />
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <system.webServer>
    <modules>
      <remove name="FormsAuthentication" />
    </modules>
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.1.0.0" newVersion="2.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.0.0" newVersion="5.2.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

3 个答案:

答案 0 :(得分:4)

几天前这对我来说是个问题。

Web分布式创作和版本控制(WebDAV)导致我的Web API不允许放置请求和删除请求。无论调试模式如何,我每次发出请求时都会收到405方法不允许。

将此添加到您的Web.Config

<modules>
  <remove name="WebDAVModule" />
</modules>

<handlers>
  <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
  <remove name="OPTIONSVerbHandler" />
  <remove name="TRACEVerbHandler" />
  <remove name="WebDAV" />
</handlers>

然后按照以下步骤操作:

  • 在IIS管理器中,单击您的网站,然后打开 WebDAV创作
  • 在IIS管理器的右侧,您将看到 WebDAV设置.. 打开它。
  • 在“请求过滤行为”下,将允许动词过滤设置为“假”

答案 1 :(得分:1)

我想说这不是一个骗局,因为我在此看到的每一个解决方案都说你要么改变你的服务器配置,要么乱用WebDAV。我从来没有做过任何事情,除了修改我的Web.config以使其工作(实际上我刚建立的应用程序的一个要求是它需要工作而无需手动配置服务器)。我正在使用的内容和发布的Web.config之间的唯一区别是我的应用程序的目标是4.5.1和system.webServer下的验证标记(为上下文发布整个system.webServer部分):

<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
  <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
  <remove name="OPTIONSVerbHandler" />
  <remove name="TRACEVerbHandler" />
  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>

答案 2 :(得分:0)

除了更改WebDAV之外,我还成功在API处理程序方法(在Controller类中)上使用了路由属性:

' PUT api/myclass/5
<HttpPut>
<Route("api/myclass/{id}", Name:="PutItem")>
Public Function PutItem(ByVal id As Integer, <FromBody()> ByVal item As MyClass) As IHttpActionResult
  ' handle PUT method here
End Function

为VB中的示例代码道歉,但这是我选择的ASP.NET语言:O

斯图尔特