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