ASP.NET Core 6 REST API大文件上传遇413请求实体过大问题求助
问题描述
尝试通过Postman上传大小约134MB的文件时,收到Status: 413 Request Entity Too Large错误,已在Startup类中配置FormOptions属性,但问题仍未解决。
相关截图:
- 上传文件界面:

- Postman文件上传调用:

- FormOptions配置:

解决建议
配置Kestrel服务器最大请求体大小
ASP.NET Core默认使用的Kestrel服务器有独立的请求大小限制,仅配置FormOptions无法覆盖这个限制。在Program.cs(或Startup.cs)中添加以下配置:builder.WebHost.ConfigureKestrel(options => { options.Limits.MaxRequestBodySize = 150 * 1024 * 1024; // 设置为150MB,可根据需求调整 });完善FormOptions配置
确保FormOptions的所有相关参数都已正确设置,避免遗漏:services.Configure<FormOptions>(options => { options.ValueLengthLimit = int.MaxValue; options.MultipartBodyLengthLimit = 150 * 1024 * 1024; // 匹配文件大小需求 options.MultipartHeadersLengthLimit = int.MaxValue; });检查反向代理服务的限制
如果应用部署在IIS、Nginx等反向代理后,需要同步调整代理的请求大小限制:- IIS:修改web.config,添加或更新以下节点:
<system.webServer> <security> <requestFiltering> <requestLimits maxAllowedContentLength="157286400" /> <!-- 150MB,单位为字节 --> </requestFiltering> </security> <serverRuntime uploadReadAheadSize="157286400" /> </system.webServer> - Nginx:在站点配置中添加:
client_max_body_size 150M;
- IIS:修改web.config,添加或更新以下节点:
确认Postman配置
检查Postman的设置,确保没有手动限制请求大小,同时确认请求的Content-Type已设置为multipart/form-data。
内容的提问来源于stack exchange,提问作者Su Ming Yuan
相关产品推荐
相关产品推荐

