Tag Archives: error: request entity too large

.Net Core 5.0 Upload File limit via Swagger Api report error: error: request entity too large [Three Methods]

.Net Core 5.0 The size of the uploaded file through the Swagger Api is limited, report the error: error: request entity too large

 

Solution:

1. Add the following code in 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. Add the method of adding features in the operation method

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

Personally recommend the first one, and limit the case to the configuration file.