.Net Core 5.0 Upload File sizes is limited via Swagger Api: error: request entity too large

1. Add the following code to startup

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<FormOptions>(options =>
    {
        // Set the limit to 256 MB
        options.MultipartBodyLengthLimit = 268435456;
    });
}

2. Add in program

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.ConfigureKestrel((context, options) =>
            {
                // Handle requests up to 50 MB
                options.Limits.MaxRequestBodySize = 52428800;
            })
            .UseStartup<Startup>();
        });

3. Adding a property to an action method

// Handle requests up to 50 MB
[RequestSizeLimit(52428800)]
public ActionResult<ResultDto<bool>> AddFile()
{
    ...
}

I recommend the first one and write the size limit to the configuration file.

Similar Posts: