如何在 IIS 上的 ASP.NET Core 中使用 MaxRequestBodySize 和 maxAllowedContentLength?

2024-03-12

我有一个在 IIS 上运行的 ASP.NET Core Web API。在一次操作中,我设置了IHttpMaxRequestBodySizeFeature.MaxRequestBodySize to 262144000。我已经使用 IIS 配置编辑器

  • modify ApplicationHost.config for my site by setting system.webServer/security/requestFiltering/requestLimits/maxAllowedContentLength to 4294967295 enter image description here
  • modify ApplicationHost.config for my site by setting system.webServer/serverRuntime/maxRequestEntityAllowed to 4294967295 enter image description here
  • modify Root Web.config for my site by setting system.web/httpRuntime/maxRequestLength to 2147483647 enter image description here

这些也是我选择时看到的价值观Web.config在 IIS 配置编辑器中。

该站点位于另一个充当反向代理的 IIS 站点后面。我在那里做了同样的改变。

我能够上传大于默认约 30MB 限制的文件,但每当我设置时,我都会收到以下警告IHttpMaxRequestBodySizeFeature.MaxRequestBodySize到一个大值:

增加 MaxRequestBodySize 与 IIS 限制 maxAllowedContentLength 的最大值发生冲突。内容长度大于 maxAllowedContentLength 的 HTTP 请求仍将被 IIS 拒绝。您可以通过删除 maxAllowedContentLength 值或将其设置为更高的限制来禁用该限制。

我什至查看了 ASP.NET 源代码,以验证仅当您尝试将限制设置为高于 IIS 设置时才会打印警告([1] https://github.com/dotnet/aspnetcore/blob/214df1c8ef0cce27b04fcc43d7df48b29964dee1/src/Servers/IIS/IIS/src/Core/IISHttpServer.cs#L85-L88, [2] https://github.com/dotnet/aspnetcore/blob/df712cc4a97d0bc8d573979008a5a2cc381f8117/src/Servers/IIS/IIS/src/Core/IISHttpContext.FeatureCollection.cs#L370-L373).

我究竟做错了什么?

如果相关,我将使用 Azure Pipelines 发布到 IIS。我的仓库不包含web.config,但为了更好地衡量,我尝试了以下方法web.config没有运气:

<configuration>
  <system.web>
    <httpRuntime maxRequestLength="2147483647" />
  </system.web>
  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="4294967295" />
      </requestFiltering>
    </security>
  </system.webServer>
</configuration>

事实证明,将限制设置为高于 Int32.MaxValue 的值时,ASP.NET Core 中存在多个错误。此外,必须在两个地方设置限制。看这个问题 https://github.com/dotnet/aspnetcore/issues/24555以及相关问题。总之:

  • Set the web.config value system.webServer.security.requestFiltering.maxAllowedContentLength to Int32.MaxValue
  • 在应用程序启动时,设置IISServerOptions.MaxRequestBodySize to Int32.MaxValue

这会消除警告(当然,将请求限制为 ~2GB)。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 IIS 上的 ASP.NET Core 中使用 MaxRequestBodySize 和 maxAllowedContentLength? 的相关文章

随机推荐