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

cme*_*ren 6 iis asp.net-core

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

  • 修改ApplicationHost.config设置为我的网站system.webServer/security/requestFiltering/requestLimits/maxAllowedContentLength,以4294967295 在此处输入图片说明
  • 修改ApplicationHost.config设置为我的网站system.webServer/serverRuntime/maxRequestEntityAllowed,以4294967295 在此处输入图片说明
  • 修改Root Web.config设置为我的网站system.web/httpRuntime/maxRequestLength,以2147483647 在此处输入图片说明

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

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

我能够上传大于默认 ~30MB 限制的文件,但每当我设置IHttpMaxRequestBodySizeFeature.MaxRequestBodySize为较大值时,我始终收到以下警告:

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

我什至查看了 ASP.NET 源代码,以验证警告仅在您尝试将限制设置为高于 IIS 设置([1][2])时才打印。

我究竟做错了什么?

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

<configuration>
  <system.web>
    <httpRuntime maxRequestLength="2147483647" />
  </system.web>
  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="4294967295" />
      </requestFiltering>
    </security>
  </system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)

cme*_*ren 4

事实证明,将限制设置为高于 Int32.MaxValue 的值时,ASP.NET Core 中存在多个错误。此外,必须在两个地方设置限制。请参阅此问题和链接的问题。总之:

  • web.config值设置system.webServer.security.requestFiltering.maxAllowedContentLengthInt32.MaxValue
  • 在应用程序启动时,设置IISServerOptions.MaxRequestBodySizeInt32.MaxValue

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