.net core/IIS/Kestrel 大文件上传大小设置

MultipartBodyLengthLimit 设置每个多部分正文的长度限制。 分析超出此限制的窗体部分时,会引发 InvalidDataException。 默认值为 134,217,728 (128 MB)。 使用 MultipartBodyLengthLimit 中的 Startup.ConfigureServices 设置自定义此限制:

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<FormOptions>(options =>
    {
        options.MultipartBodyLengthLimit = 1048576L * 5000L;//5G
    });
}

//

builder.Services.Configure<FormOptions>(options =>
{
    options.MultipartBodyLengthLimit = 1048576L * 5000L;//5G
});

Kestrel 最大请求正文大小

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.ConfigureKestrel((context, options) =>
            {
                options.Limits.MaxRequestBodySize = 52428800;
            })
            .UseStartup<Startup>();
        });

//

builder.WebHost.ConfigureKestrel(options =>
{
    options.Limits.MaxRequestBodySize = 1048576L * 5000L; //5G
});

//
//在 appsettings.json 中配置终结点
{
  "Kestrel": {"Limits": {
      "MaxRequestBodySize ": "100000000"
    }
  }
}

IIS默认的请求限制 (maxAllowedContentLength) 为 30,000,000 个字节,约为 28.6 MB,最大可设置4294967295 MB(4G)。 在 web.config 文件中自定义此限制:

<system.webServer>
  <security>
    <requestFiltering>
      <requestLimits maxAllowedContentLength="4294967295" />
    </requestFiltering>
  </security>
</system.webServer>

 

posted @ 2025-03-09 13:23  anech  阅读(81)  评论(0)    收藏  举报