Jac*_*ack 5 iis asp.net-mvc .net-core
我正在尝试将大文件上传到我的 ASP.net core/ASP.net mvc 站点,但不断遇到 HTTP 413 - 请求实体太大。即使当我尝试更新 web.config 时,其他事情仍然会出现问题。有人有处理这个问题的备忘单吗?
小智 16
我创建了一个 asp.net mvc 应用程序并尝试测试它。
很多时候我们只关注uploadReadAheadSize的设置,但测试证明这并不能完全解决问题,还有其他的配置需要配置。(根据Jack的回答添加)
除了将uploadReadAheadSize值设置得较大之外,还有maxAllowedContentLength和maxRequestLength。您需要修改所有这三个值,否则仍然会显示
413或超出最大请求长度错误。

在我的测试结果中,该应用程序可以上传1GB以内的任何类型的文件。
这是我的备忘单:
来自 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>
Run Code Online (Sandbox Code Playgroud)
只要这个说明符确实有效,那就太棒了。例如,如果您使用 IIS 以外的其他设备(例如 kestrel,甚至 IIS Express)进行本地托管,您可能需要不同的解决方案。
有时此配置部分也会被锁定,在这种情况下可以使用以下命令解锁(以管理员身份运行 cmd):
%windir%\system32\inetsrv\appcmd.exe unlock config -section:system.webServer/serverRuntime
Run Code Online (Sandbox Code Playgroud)
这也是设置产品时要考虑的事情。在某些情况下,您代码中的 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
Run Code Online (Sandbox Code Playgroud)
我还在产品服务器上的 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>
Run Code Online (Sandbox Code Playgroud)
也可以按照此屏幕截图进行设置:
[![在此处输入图像描述][1]][1]
现在您已经完成了这些设置,您的应用程序仍然完全有可能崩溃,从而出现 413 错误,因为即使 IIS 可能设置正确,.net core 生态系统中也存在上传限制,这可能会引发 HTTP 414 错误处理大文件时。这是我在[这里][2]发现的
简单来说,您希望在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;
});
Run Code Online (Sandbox Code Playgroud)
请注意,如果您托管的是 kestrel 而不是 IIS,则其中有代码 [1]:https: //i.stack.imgur.com/yLHQQ.png [2]: https: //github.com/ dotnet/aspnetcore/issues/20369#issuecomment-607057822
| 归档时间: |
|
| 查看次数: |
13632 次 |
| 最近记录: |