【问题标题】:HTTP 413 - Request Entity Too Large cheat sheetHTTP 413 - 请求实体太大备忘单
【发布时间】:2021-03-05 20:08:37
【问题描述】:

我正在尝试将一个大文件上传到我的 ASP.net core/ASP.net mvc 站点,但不断遇到 HTTP 413 - 请求实体太大。即使我尝试更新 web.config,其他事情仍然会中断。 有人有处理这个问题的备忘单吗?

【问题讨论】:

    标签: asp.net-mvc iis .net-core


    【解决方案1】:

    我创建了一个 asp.net mvc 应用程序并尝试对其进行测试。

    很多时候我们只关注uploadReadAheadSize设置,但测试证明这样并不能完全解决问题,还有其他配置需要配置。 (根据杰克的回答添加)

    除了将uploadReadAheadSize值设置得大一些,还有ma​​xAllowedContentLengthma​​xRequestLength。这三个值都需要修改,否则仍然会显示413Maximum request length exceeded错误。

    在我的测试结果中,应用可以上传1GB以内的任何类型的文件。

    【讨论】:

      【解决方案2】:

      这是我的备忘单:

      来自 IIS 的最大文件大小限制是通过 UploadReadAhaead 参数指定的,该参数可以在 web.config 中指定。在这种情况下,最大值设置为 1000485760 字节

      <?xml version="1.0" encoding="utf-8"?> 
      <configuration>
        <system.webServer>
          <httpErrors errorMode="Detailed"></httpErrors>
          <serverRuntime uploadReadAheadSize="1000485760" />
          <httpErrors errorMode="Detailed" />
          <asp scriptErrorSentToBrowser="true"/>
          </system.webServer>
      <system.web>
          <customErrors mode="Off"/>
          <compilation debug="true"/>
      </system.web>
      </configuration>
      

      这很好,只要这个说明符确实有效。您可能需要不同的解决方案,例如,如果您使用 IIS 以外的东西(例如 kestrel,甚至 IIS express)在本地托管。

      有时此配置部分也被锁定,在这种情况下,可以使用以下命令将其解锁(以管理员身份运行 cmd):

      %windir%\system32\inetsrv\appcmd.exe unlock config -section:system.webServer/serverRuntime
      

      这也是设置 prod 时需要考虑的问题。在某些情况下,您的代码中 web.config 中用于 uploadReadAheadSize 的配置可能与您的产品实例中指定的任何内容发生冲突。对我来说,我最终摆脱了源代码控制中的配置,而是在环境设置期间为 prod 设置了 uploadReadAhead 值。我通过运行命令(在 cmd 中作为管理员)来做到这一点:

      appcmd set config "https://example.com" /section:system.webserver/serverruntime /uploadreadaheadsize:500048576 /commit:apphost
      appcmd set config "http://example.com" /section:system.webserver/serverruntime /uploadreadaheadsize:500048576 /commit:apphost
      

      我还在 prod 服务器上的 C:\inetpub\wwwroot\web.config 中设置了 uploadReadAheadSize,所以它看起来像:

      <?xml version="1.0" encoding="utf-8"?>
      <configuration>
        <system.webServer>
          <serverRuntime uploadReadAheadSize="500485760" />
          <!--If this line breaks, unlock the config by opening cmd as admin and running %windir%\system32\inetsrv\appcmd.exe unlock config -section:system.webServer/serverRuntime-->
        </system.webServer>
      </configuration>
      

      这也可以按照这个截图来设置:

      [![在此处输入图片描述][1]][1]

      现在您已经设置好了,您的应用程序仍然完全有可能中断,给您一个 413,因为 即使 IIS 可能设置正确,.net 核心生态系统中也存在上传限制处理大文件时可能会引发 HTTP 414 错误。 这是我发现的 [这里][2]

      TLDR 是您希望在您的 startup.cs 中包含以下代码 -> configureServices()

          services.Configure<IISServerOptions>(options => {
              options.MaxRequestBodySize = int.MaxValue;
          });
      
          services.Configure<FormOptions>(x =>
          {
              x.ValueLengthLimit = int.MaxValue;
              x.MultipartBodyLengthLimit = int.MaxValue;
              x.BufferBodyLengthLimit = int.MaxValue;
              x.MultipartBoundaryLengthLimit = int.MaxValue;
          });
      

      请注意,如果您托管的是 kestrel 而不是 IIS,则其中还有代码 [1]:https://i.stack.imgur.com/yLHQQ.png [2]:https://github.com/dotnet/aspnetcore/issues/20369#issuecomment-607057822

      【讨论】:

        猜你喜欢
        • 2012-09-23
        • 1970-01-01
        • 2014-12-30
        • 2014-12-23
        • 2015-10-04
        • 2015-01-25
        • 2014-12-04
        • 2017-08-28
        相关资源
        最近更新 更多