如何在带有maxAllowedContentLength的IIS上的ASP.NET Core中使用MaxRequestBodySize? [英] How to use MaxRequestBodySize in ASP.NET Core on IIS with maxAllowedContentLength?

查看:290
本文介绍了如何在带有maxAllowedContentLength的IIS上的ASP.NET Core中使用MaxRequestBodySize?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an ASP.NET Core web API running on IIS. In an operation, I set IHttpMaxRequestBodySizeFeature.MaxRequestBodySize to 262144000. I have used the IIS Configuration Editor to

    通过将 system.webServer/security/requestFiltering/requestLimits/maxAllowedContentLength 设置为 4294967295 通过将 system.webServer/serverRuntime/maxRequestEntityAllowed 设置为 4294967295 通过将 system.web/httpRuntime/maxRequestLength 设置为 2147483647
  • modify ApplicationHost.config for my site by setting system.webServer/security/requestFiltering/requestLimits/maxAllowedContentLength to 4294967295
  • modify ApplicationHost.config for my site by setting system.webServer/serverRuntime/maxRequestEntityAllowed to 4294967295
  • modify Root Web.config for my site by setting system.web/httpRuntime/maxRequestLength to 2147483647

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

Those are the values I also see when I choose Web.config in the IIS Configuration Editor.

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

The site is behind another IIS site acting as a reverse proxy. I have made the same changes there.

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

I am able to upload files larger than the default ~30MB limit, but I consistently get the following warning whenever I set IHttpMaxRequestBodySizeFeature.MaxRequestBodySize to a large value:

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

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设置时才显示警告(

I have even looked at the ASP.NET source code to verify that the warning only prints if you're trying to set the limit higher than the IIS settings ([1], [2]).

我在做什么错了?

如果相关,我将使用Azure Pipelines发布到IIS.我的存储库不包含 web.config ,但是在很大程度上,我尝试了以下 web.config 而不是运气:

If it's relevant, I am publishing to IIS using Azure Pipelines. My repo does not contain a web.config, but for good measure I tried with the following web.config without luck:

<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中存在多个错误.此外,该限制必须在两个地方设置.请参阅此问题和链接的问题.总结:

Turns out there are several bugs in ASP.NET Core when setting the limits to values above Int32.MaxValue. Furthermore, the limit must be set in two places. See this issue and linked issues. In summary:

  • web.config system.webServer.security.requestFiltering.maxAllowedContentLength 设置为 Int32.MaxValue
  • 在应用启动时,将 IISServerOptions.MaxRequestBodySize 设置为 Int32.MaxValue
  • Set the web.config value system.webServer.security.requestFiltering.maxAllowedContentLength to Int32.MaxValue
  • In the app startup, set IISServerOptions.MaxRequestBodySize to Int32.MaxValue

这消除了警告(并且当然将请求限制在2GB之内).

That removes the warning (and of course limits requests to ~2GB).

这篇关于如何在带有maxAllowedContentLength的IIS上的ASP.NET Core中使用MaxRequestBodySize?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆