How do you configure and enforce a maximum request/file upload size in ASP.NET Core, and what's the default limit?
Short answer
Kestrel's default MaxRequestBodySize is 30MB; configure it via KestrelServerOptions.Limits.MaxRequestBodySize globally, or override per-endpoint via [RequestSizeLimit(bytes)] on a controller action, or disable the limit entirely with [DisableRequestSizeLimit] for endpoints…
Advertisement
AModel answer
Kestrel's default MaxRequestBodySize is 30MB; configure it via KestrelServerOptions.Limits.MaxRequestBodySize globally, or override per-endpoint via [RequestSizeLimit(bytes)] on a controller action, or disable the limit entirely with [DisableRequestSizeLimit] for endpoints specifically designed to accept very large uploads (used carefully, combined with other safeguards against abuse).
builder.WebHost.ConfigureKestrel(options => {
options.Limits.MaxRequestBodySize = 100_000_000; // 100MB globally
});
[RequestSizeLimit(50_000_000)] // 50MB override for this specific action
[HttpPost("upload")]
public async Task<IActionResult> Upload(IFormFile file) { ... }Real-world example
A video-sharing platform sets a global 500MB Kestrel limit but overrides a specific low-risk thumbnail-upload endpoint down to a much stricter 5MB via [RequestSizeLimit], preventing that particular endpoint from being abused for oversized uploads.
Likely follow-ups
- How does this limit interact with a reverse proxy (like nginx) that might have its own separate size limit?
- What HTTP status code is returned when the limit is exceeded?
Related topics