ASP.NET IIS 核心 - 不允许使用 HTTP 动词

ASP.NET Core with IIS - HTTP Verb Not Allowed

我们有一个 ASP.NET Core 2.0 网站,该网站还提供了一些简单的 Web API用于 UI 增强目的的方法。

Web API 调用在 运行 本地 IIS Express 下按预期工作,但是当我们部署到我们的 IIS 8.5 生产 Web 服务器,我们在发出 HTTP DELETEPUT 请求时收到以下错误...

405 Method Not Allowed

经过一些网络搜索,我们发现有几篇帖子建议删除 IIS WebDAV 模块。我们已经在 IIS(它是我们的服务器)中禁用了它,我们还尝试了以下方法:

None 以上步骤已经解决了我们的问题。

任何 advice/direction 将不胜感激。

经过数小时的研究和反复试验,修复似乎非常简单。将 Web.config 文件添加到您的 .NET Core 2.0 应用程序:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <!-- To customize the asp.net core module uncomment and edit the following section. 
         For more info see https://go.microsoft.com/fwlink/?linkid=838655 -->
    <system.webServer>
        <modules>
            <remove name="WebDAVModule" />
        </modules>
        <handlers>
            <remove name="aspNetCore" />
            <remove name="WebDAV" />
            <!-- I removed the following handlers too, but these
                 can probably be ignored for most installations -->
            <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
            <remove name="OPTIONSVerbHandler" />
            <remove name="TRACEVerbHandler" />

            <add name="aspNetCore" 
                 path="*" 
                 verb="*" 
                 modules="AspNetCoreModule" 
                 resourceType="Unspecified" />
        </handlers>
        <aspNetCore processPath="%LAUNCHER_PATH%" 
                    arguments="%LAUNCHER_ARGS%" 
                    stdoutLogEnabled="false"
                    stdoutLogFile=".\logs\stdout" />
    </system.webServer>
</configuration>

希望对您有所帮助。

这对我有用 (netcore 2.0)

<system.webServer>
  <modules runAllManagedModulesForAllRequests="false">
    <remove name="WebDAVModule" />
  </modules>
</system.webServer>

在此处找到:https://www.ryadel.com/en/error-405-methods-not-allowed-asp-net-core-put-delete-requests/

要完全阻止 WebDav 被启用,ApplicationHost.config 中删除 以下条目:

<add name="WebDAVModule" /> 

条目位于模块部分。

配置的确切位置:
C:\Windows\System32\inetsrv\config\applicationHost.config

这在 .Net Core 2.1 中对我来说效果很好

从 IIS 服务器删除 WebDAV 发布。它属于 Internet 信息服务 -> 通用 Http 功能

https://ignas.me/tech/405-method-not-allowed-iis/

对我来说,在我注意到我试图 post 而不是 API 到客户端域后,我解决了这个问题。 [捂脸]

虽然删除 WebDAV 可能会解决您的问题,但您可能想知道(就像我一样)什么是 WebDAV 以及它的用途是什么?

我找到了有助于解释它的页面:

https://www.cloudwards.net/what-is-webdav/

要完全阻止 WebDav 启用,请从 ApplicationHost.config 中删除或评论以下条目:

<add name="WebDAVModule" />

该条目位于模块部分。

配置的确切位置: C:\Windows\System32\inetsrv\config\applicationHost.config

这在 .Net Core 2.2 中对我来说效果很好

就我而言,.Net 5 Web API PUT 和 DELETE 调用出现 405 错误。我尝试了多种解决方案,例如删除 WebDAV(打开或关闭 Windows 功能 -> IIS -> 万维网服务 -> 通用 HTTP 功能 -> WebDAV 发布)不起作用,编辑“处理程序映射”中的 WebDAV 条目弄乱了应用程序。

解决方案

在 IIS 中,select 应用程序

  1. 添加规则以允许在请求过滤中使用 HTTP 谓词(但仅此一项不起作用)。
  2. 转到“模块”,然后select“WebDAV Publishing”模块并将其删除。
  3. 转到“处理程序映射”,然后select“WebDAV”并将其删除。
  4. 在cmd中运行 IISRESET

将以下行添加到您的 web.config 文件中。就是这样。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
        <modules>
            <remove name="WebDAVModule" />
        </modules>
      <handlers>
      
            <remove name="WebDAV" />
            <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
            <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,PUT,DELETE,DEBUG" type="System.Web.Handlers.TransferRequestHandler" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" responseBufferLimit="0" />
            
            <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="dotnet" arguments=".\Ftms.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
    </system.webServer>
  </location>
</configuration>

就我而言,我在将 requireAccess="None" 添加到 aspNetCore 处理程序后解决了问题

像这样:

<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" requireAccess="None" />

我什至没有在 IIS 中的处理程序映射下找到 aspnetcore 的映射。它让我打开 Visual Studio Installer 并安装 Development time IIS support under .NET cross-platform development。然后,aspnetcore 处理程序映射出现在 IIS 中。