.net core 上传文件大小限制 webconfig

1 发布后,修改webconfig文件

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".TAX.WebAPI.dll" stdoutLogEnabled="false" stdoutLogFile=".logsstdout" />
<security>

<requestFiltering>

<requestLimits maxAllowedContentLength="1073741822" /><!-- 1GB-->

</requestFiltering>

</security>
</system.webServer>
</location>

</configuration>
<!--ProjectGuid: B5091FF6-AC7A-47D5-8BF3-8604AECA5211-->

2.在Startup的ConfigureServices中添加代码段

  1. //上传文件大小限制Kestrel设置
  2. services.Configure<KestrelServerOptions>(options =>
  3. {
  4. // Set the limit to 256 MB
  5. options.Limits.MaxRequestBodySize = 268435456;
  6. });
  7. //上传文件大小限制IIS设置
  8. services.Configure<IISServerOptions>(options =>
  9. {
  10. options.MaxRequestBodySize = long.Parse(Configuration.GetSection("Kestrel").Value);
 
});
 
3 -------
 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;
            });
        }
 
原文地址:https://www.cnblogs.com/zhang-wenbin/p/10412442.html