# Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException：请求正文过大。最大请求正文大小为 30000000 字节。

- **ID:** `dotnet/kestrel-request-body-too-large`
- **领域:** dotnet
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| ASP.NET Core 6.0 | active | — | — |
| ASP.NET Core 7.0 | active | — | — |
| ASP.NET Core 8.0 | active | — | — |

## 解决方案

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
   ```

## 无效尝试

- **** — This disables the limit entirely, risking denial-of-service from large payloads. (80% 失败率)
- **** — Kestrel checks the uncompressed size; compression does not bypass the limit. (70% 失败率)
