项目场景:
在IIS下部署的ASP.Net Core系统,在上传53MB的文件时,出现上传失败,返回异常状态码413.经过调查,发现是有两层限制:一个是ASP.Net Core框架级别限制,另一个是IIS限制
解决方案:
1、IIS限制解除,修改Web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<security>
<requestFiltering>
<!-- 2 GB -->
<requestLimits maxAllowedContentLength="2147483648" />
</requestFiltering>
</security>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
</aspNetCore>
</system.webServer>
</configuration>
2、ASP.Net Core 限制接触
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(config => config.Filters.Add<AuthFilter>())
.AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver())
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
//解决文件上传Multipart body length limit 134217728 exceeded
services.Configure<FormOptions>(x =>
{
x.ValueLengthLimit = int.MaxValue;
x.MultipartBodyLengthLimit = int.MaxValue;
x.MemoryBufferThreshold = int.MaxValue;
});
}
或者针对单独API接触限制
调整或禁用程序的上传限制:
禁用:
[DisableRequestSizeLimit]
调整大小:
[RequestSizeLimit(100_000_000)
写在指定的API上面