方法1,
全局避免
引入包 dotnet add package Microsoft.AspNetCore.Http.Features
using Microsoft.AspNetCore.Http.Features;public void ConfigureServices(IServiceCollection services)
{services.Configure<FormOptions>(options =>{// 设置允许的最大请求体大小options.MultipartBodyLengthLimit = 60000000; // 60 MBoptions.ValueLengthLimit = 60000000; // 60 MBoptions.MemoryBufferThreshold = 60000000; // 60 MB});// 其他配置和服务注册...
}
方法2
针对某个方法
[HttpPost("specific-method")][RequestSizeLimit(10_000_000)] // 10 MBpublic IActionResult PostSpecificMethod([FromBody] MyModel model){// 处理请求return Ok();}