dotnet runtime_error ai_generated true

Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException: Request body too large. The max request body size is 30000000 bytes.

ID: dotnet/kestrel-request-body-too-large

Also available as: JSON · Markdown · 中文
90%Fix Rate
89%Confidence
1Evidence
2023-02-14First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
ASP.NET Core 6.0 active
ASP.NET Core 7.0 active
ASP.NET Core 8.0 active

Root Cause

The incoming HTTP request body exceeds the Kestrel server's configured maximum request body size limit, which defaults to ~28.6 MB (30,000,000 bytes) or can be customized via MaxRequestBodySize.

generic

中文

传入的 HTTP 请求正文超过了 Kestrel 服务器配置的最大请求正文大小限制,默认约为 28.6 MB(30,000,000 字节),或可通过 MaxRequestBodySize 自定义。

Official Documentation

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel/options

Workarounds

  1. 90% success Increase the limit for specific endpoints using the [RequestSizeLimit] attribute: [RequestSizeLimit(50_000_000)] public IActionResult Upload() { ... }
    Increase the limit for specific endpoints using the [RequestSizeLimit] attribute: [RequestSizeLimit(50_000_000)] public IActionResult Upload() { ... }
  2. 95% success Configure Kestrel globally in Program.cs: builder.WebHost.ConfigureKestrel(options => options.Limits.MaxRequestBodySize = 50 * 1024 * 1024); // 50 MB
    Configure Kestrel globally in Program.cs: builder.WebHost.ConfigureKestrel(options => options.Limits.MaxRequestBodySize = 50 * 1024 * 1024); // 50 MB

中文步骤

  1. 使用 [RequestSizeLimit] 属性增加特定端点的限制:[RequestSizeLimit(50_000_000)] public IActionResult Upload() { ... }
  2. 在 Program.cs 中全局配置 Kestrel:builder.WebHost.ConfigureKestrel(options => options.Limits.MaxRequestBodySize = 50 * 1024 * 1024); // 50 MB

Dead Ends

Common approaches that don't work:

  1. 80% fail

    This disables the limit entirely, risking denial-of-service from large payloads.

  2. 70% fail

    Kestrel checks the uncompressed size; compression does not bypass the limit.