Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increase the request size limit using minimal api with .NET 6?

I have an endpoint to upload videos

app.MapPost("/video/upload", async (HttpRequest request) =>

It works with small videos but when I try to upload a video of larger size I get an error saying that I exceeded the allowed size

like image 771
Careuno Merchan Avatar asked Oct 18 '25 06:10

Careuno Merchan


2 Answers

You can use IHttpMaxRequestBodySizeFeature

async (HttpRequest request) =>
{
    var bodySizeFeature = 
    request.HttpContext.Features.Get<IHttpMaxRequestBodySizeFeature>();
    if (bodySizeFeature is not null)
    {
        bodySizeFeature.MaxRequestBodySize = 10; // set limit or null for unlimited
    }
        
    // process request
}
like image 84
Andrei Avatar answered Oct 20 '25 22:10

Andrei


We can configure kestrel through the builder value and specify the max request body size

var builder = WebApplication.CreateBuilder(args);

builder.WebHost.UseKestrel(o => o.Limits.MaxRequestBodySize = null);

null means that there is no limit.

like image 31
Careuno Merchan Avatar answered Oct 21 '25 00:10

Careuno Merchan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!