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

标签 asp.net-core iis

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

  • 修改 ApplicationHost.config为我的网站设置 system.webServer/security/requestFiltering/requestLimits/maxAllowedContentLength4294967295 enter image description here
  • 修改 ApplicationHost.config为我的网站设置 system.webServer/serverRuntime/maxRequestEntityAllowed4294967295 enter image description here
  • 修改 Root Web.config为我的网站设置 system.web/httpRuntime/maxRequestLength2147483647 enter image description here

  • 这些是我在选择 Web.config 时也看到的值在 IIS 配置编辑器中。
    该站点位于另一个充当反向代理的 IIS 站点之后。我在那里做了同样的改变。
    我可以上传大于默认 ~30MB 限制的文件,但是每当我设置 IHttpMaxRequestBodySizeFeature.MaxRequestBodySize 时,我始终收到以下警告到一个很大的值:

    Increasing the MaxRequestBodySize conflicts with the max value for IIS limit maxAllowedContentLength. HTTP requests that have a content-length greater than maxAllowedContentLength will still be rejected by IIS. You can disable the limit by either removing or setting the maxAllowedContentLength value to a higher limit.


    我什至查看了 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>
    

    最佳答案

    事实证明,当将限制设置为高于 Int32.MaxValue 的值时,ASP.NET Core 中存在几个错误。此外,必须在两个地方设置限制。见 this issue和相关问题。总之:

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

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

    关于asp.net-core - 如何在具有 maxAllowedContentLength 的 IIS 上的 ASP.NET Core 中使用 MaxRequestBodySize?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63172966/

    相关文章:

    asp.net-core - 从 ASP.NET Core 2.2 升级到 3.0

    c# - 字符串变量的值无法转换为整数

    asp.net - 如何撤销/无效/取消旧的电子邮件确认 token (身份)

    c# - Blazor azure AD 从图形 API 检索电子邮件

    c# - 获取网站服务的 tcp 端口

    authentication - 本地ADFS流的正确身份验证设置是什么?

    c# - 是否可以为 ASPNET.Core OData 声明多个路由

    asp.net - 一个 IIS 服务器中的 JavaScript 问题,但不是另一个

    iis - LESS 文件无法加载 (404)

    c# - IIS 10 上的 ASP.NET Core 404 错误