我想在 ASP.NET Core 2.0 Web 应用程序中增加 30MB 的默认上传大小。建议的解决方案如添加 [RequestSizeLimit(4_100_000_000)] 或添加
services.Configure<FormOptions>(x => x.MultipartBodyLengthLimit = 4074790400);
不适用于 asp.net core 2.0。我猜这些解决方案适用于 .NET Core 1+ 版本。
我也在 Program.cs 中添加了这个,这也是建议的解决方案:
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseKestrel(options =>
{
options.Limits.MaxRequestBodySize = 4120100000;
})
.Build();
尽管在本地 - 使用 Visual Studio docker 调试器,我可以上传大于 30 MB 的文件,但在 AWS 环境中,我再次收到 413 Request Entity Too Large 错误。
最佳答案
在 aws 18.04 ubuntu 上有同样的问题,运行 .net core 2.2 和 nginx 1.16.1 作为前端服务器和反向代理。
通过做两件事来解决这个问题:
1)配置nginx的最大请求大小 -
确保前端 nginx 不会阻止请求到达反向代理后面的应用程序。
为此,我添加了 'adding client_max_body_size 30M; '在服务器块内,
但还有更多方法可以做到这一点,请参阅:https://www.tecmint.com/limit-file-upload-size-in-nginx/ )
2) 将相同的代码添加到 Main,有 1 个小的变化 - 我没有指定请求大小,而是插入了一个空值,这会导致 kestrel 假设它是最大的可能值(afaik)
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseKestrel(options =>
{
options.Limits.MaxRequestBodySize = null;
})
.Build();
关于c# - 如何在asp.net core 2+中增加上传大小?请求的实体太大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53151273/