1、在Startup中添加以下代码
public void ConfigureServices(IServiceCollection services) { services.Configure<FormOptions>(options => { // Set the limit to 256 MB options.MultipartBodyLengthLimit = 268435456; }); }
2、在Program中添加
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.ConfigureKestrel((context, options) => { // Handle requests up to 50 MB options.Limits.MaxRequestBodySize = 52428800; }) .UseStartup<Startup>(); });
3、在操作方法中添加特性的方法添加
// Handle requests up to 50 MB [RequestSizeLimit(52428800)] public ActionResult<ResultDto<bool>> AddFile() { ... }
个人推荐第一种,并将限制大小写到配置文件中。