简体   繁体   中英

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

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.

This can be configured in the web.config file.

<system.webServer>
    <security>
        <requestFiltering>
            <requestLimits maxAllowedContentLength="1073741824" />
        </requestFiltering>
    </security>
</system.webServer>

This will set the limit to 1GB

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM