.NET7+Angular大文件上传遇HTTP 413.1错误,求解决方案
大文件上传HTTP 413.1错误排查(.NET7 + Angular + ng2-file-upload)
我开发了一个Angular应用,使用ng2-file-upload组件向.NET7后端上传文件,上传大文件时持续返回HTTP 413.1错误,推测是服务端配置问题,此处提及客户端组件供参考。
我的后端是.NET7 Web应用,已将Angular编译产物放入wwwroot目录,并添加web.config配置重写规则以整合运行。目前已做如下配置:
1. web.config配置
<system.webServer> <security> <requestFiltering> <requestLimits maxAllowedContentLength="50000000" /> </requestFiltering> </security> <serverRuntime uploadReadAheadSize="50000000" /> </system.webServer> <system.web> <httpRuntime maxRequestLength="50000" /> </system.web>
2. 上传接口特性配置
[RequestSizeLimit(50_000_000)] // 或 [DisableRequestSizeLimit]
3. Program.cs配置
var builder = WebApplication.CreateBuilder(args); /* ..... */ builder.Services .Configure<FormOptions>(x => { x.ValueLengthLimit = 50000000; x.MultipartBodyLengthLimit = 50000000; // 针对multipart类型请求 x.MultipartHeadersLengthLimit = 50000000; }) .Configure<IISServerOptions>(x => { x.MaxRequestBodySize = 50000000; }); /* ..... */ var app = builder.Build() /* ..... */ app.Use(async (context, next) => { var feature = context.Features.Get<IHttpMaxRequestBodySizeFeature>(); if (feature != null) feature.MaxRequestBodySize = 50000000; await next.Invoke(); });
疑问
- 还有哪些配置遗漏?
- 是否需额外设置使web.config生效?
- C#配置是否需特定顺序才能生效?
- 当前在VS2022的IIS Express开发环境调试,后续需部署至Azure App Service,是否有适配要点?
内容的提问来源于stack exchange,提问作者Matt James
相关产品推荐
相关产品推荐

