405方法不允许在ASP.NET WebAPI中删除PUT和删除



我已经使用ASP.NET Web API构建了一些API端点。当我尝试执行 put delete 请求时,get 405方法不允许错误。但是对于 get post 请求。

有趣的是,当我在本地IIS托管该项目时,PUT和DELETE运行良好。仅在 dev 服务器中投掷 405 错误。

在我的 web.config 中:

    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2" />
    <httpModules>
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
    </httpModules>
  </system.web>
  <system.webServer>
    <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>
    <validation validateIntegratedModeConfiguration="false" />
    <modules>
      <remove name="ApplicationInsightsWebTracking" />
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
    </modules>
  </system.webServer>

我在这里想念什么?预先感谢。

如果您使用的是Visual Studio 2012或更高版本来开发Web API应用程序,IIS Express是默认的Web服务器。该Web服务器是服务器产品中发货的完整IIS功能的缩放版本,该Web服务器包含一些用于开发方案的更改。例如,WebDAV模块通常安装在运行IIS完整版本的生产Web服务器上,尽管它可能不是实际使用。

IIS(IIS Express)的开发版本安装了WebDav模块,,但是WebDav模块的条目有意发表,因此WebDAV模块永远不会在IIS Express 上加载。结果,您的Web应用程序可以在本地计算机上正确工作,但是当您将Web API应用程序发布到生产Web服务器时,您可能会遇到HTTP 405错误。

由于您的开发服务器具有完整的IIS功能(使用WebDav),它将为同一动词/方法注册多个处理程序,其中一位处理程序阻止了预期的处理程序处理请求。

因此,WebDav正在覆盖您的HTTP放置和删除。在处理HTTP PUT请求的过程中,IIS调用WebDAV模块,当调用WebDav模块时,它会检查其配置并发现其已禁用,因此它将返回HTTP 405方法不允许使用类似于WebDav请求的任何请求的错误。

您可以在此处阅读更多信息https://learn.microsoft.com/en-us/aspnet/web-papi/web-api/popi/testing-and-debugging/troubleshooting-http-405-405-erors-erors-erors-erors-erors-erors-erors-erors-erors-erors-erors-rast-publist-publist--publishishweb--publishish-web--------Api Applications

禁用WebDav将以下内容添加到您的web.config

<system.webServer>
 <modules>
  <remove name="WebDAVModule"/>
 </modules>
 <handlers>
  <remove name="WebDAV" />
 </handlers>
</system.webServer>

最新更新