You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置IIS允许HttpDelete且不移除WebDAVModule

解决ASP.NET Core Web API中HttpDelete被WebDAV拦截的问题

不需要直接移除WebDAVModule,通过以下几种配置方案,既能保留WebDAV的原有功能,又能让HttpDelete请求正常工作:

方案1:让WebDAV忽略API路径的请求

在web.config的<system.webServer>节点下添加配置,排除API路径的WebDAV处理:

<system.webServer>
  <modules>
    <remove name="WebDAVModule" />
  </modules>
  <handlers>
    <remove name="WebDAV" />
    <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
  </handlers>
  <aspNetCore processPath="dotnet" arguments=".\YourApi.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
  <!-- 配置WebDAV仅作用于非API路径,示例API前缀为/api -->
  <webdav>
    <authoringRules>
      <add path="*" verbs="GET,PUT,POST,PROPFIND,PROPPATCH,MKCOL,COPY,MOVE,DELETE,LOCK,UNLOCK" users="*" allowUnspecified="true" />
      <remove path="/api/*" />
    </authoringRules>
  </webdav>
</system.webServer>

WebDAV会跳过对/api路径下的所有请求,你的Delete请求就能被ASP.NET Core正常接收处理。

方案2:调整处理器顺序,让ASP.NET Core优先处理API请求

通过修改web.config中处理器的顺序,让ASP.NET Core先接管API路径的请求,WebDAV仅处理其他路径:

<system.webServer>
  <modules>
    <!-- 保留WebDAVModule,调整其运行条件 -->
    <add name="WebDAVModule" type="Microsoft.Web.WebDav.Server.WebDavModule, Microsoft.Web.Dav.Server" preCondition="managedHandler" />
  </modules>
  <handlers>
    <remove name="WebDAV" />
    <!-- 优先让ASP.NET Core处理API路径的所有请求 -->
    <add name="aspNetCore" path="/api/*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
    <!-- WebDAV处理剩余路径的请求 -->
    <add name="WebDAV" path="*" verb="GET,PUT,POST,PROPFIND,PROPPATCH,MKCOL,COPY,MOVE,DELETE,LOCK,UNLOCK" modules="WebDAVModule" resourceType="Unspecified" requireAccess="Write" />
  </handlers>
</system.webServer>

这种方式可以避免WebDAV和ASP.NET Core的请求处理冲突。

方案3:通过ASP.NET Core中间件解除Delete请求限制

在项目的启动文件(.NET 6+为Program.cs,旧版本为Startup.cs)中添加自定义中间件,提前处理Delete请求,移除可能被WebDAV添加的限制头:

// .NET 6+ Program.cs示例
var builder = WebApplication.CreateBuilder(args);

// 添加所需服务...

var app = builder.Build();

// 在其他中间件之前添加此逻辑
app.Use(async (context, next) =>
{
    if (context.Request.Method.Equals(HttpMethod.Delete.Method, StringComparison.OrdinalIgnoreCase))
    {
        context.Response.Headers.Remove("Allow");
    }
    await next();
});

// 其他中间件配置
app.MapControllers();

app.Run();

该中间件会确保Delete请求不受WebDAV的头限制,顺利到达你的控制器。

内容的提问来源于stack exchange,提问作者Anuj Karki

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 13:07:18