Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException: Request body too large

17,664

Solution 1

I think you just need: [DisableRequestSizeLimit]

below is a solution that worked for me to upload Zip files with additional form data to an API running .Net Core 3

// MultipartBodyLengthLimit  was needed for zip files with form data.
// [DisableRequestSizeLimit] works for the KESTREL server, but not IIS server 
// for IIS: webconfig... <requestLimits maxAllowedContentLength="102428800" />
[RequestFormLimits(ValueLengthLimit = int.MaxValue, MultipartBodyLengthLimit = int.MaxValue)] 
[DisableRequestSizeLimit] 
[Consumes("multipart/form-data")] // for Zip files with form data
[HttpPost("MyCustomRoute")]
public IActionResult UploadZippedFiles([FromForm] MyCustomFormObject formData)
{ }

Solution 2

NOTE: This is issue I faced when I migrated my application from asp.net core 2.1 to 3.0

To fix this in asp.net core 3.0 I have changed my program.cs to modify maximum request body size like below.

public class Program
{
    public static void Main(string[] args)
    {
       CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args)
    {
       return WebHost.CreateDefaultBuilder(args)
            .ConfigureKestrel((context, options) =>
            {
                options.Limits.MaxRequestBodySize = 737280000;
            })
            .UseStartup<Startup>();
        }
    }
}

I mean I have just added ConfigureKestrel part and added an attribute above my action method [RequestSizeLimit(737280000)] like below

[HttpPost]
[RequestSizeLimit(737280000)]
[Route("SomeRoute")]
public async Task<ViewResult> MyActionMethodAsync([FromForm]MyViewModel myViewModel)
{
   //Some code
   return View();
}

And my application started behaving correctly again without throwing BadHttpRequestException: Request body too large

reference: https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-3.0#kestrel-maximum-request-body-size

Solution 3

For me (Asp.net core 3.1) the solution was to add these lines in ConfigureServices method of Startup.cs:

    // 200 MB
    const int maxRequestLimit = 209715200;
    // If using IIS
    services.Configure<IISServerOptions>(options =>
    {
        options.MaxRequestBodySize = maxRequestLimit;
    });
    // If using Kestrel
    services.Configure<KestrelServerOptions>(options =>
    {
        options.Limits.MaxRequestBodySize = maxRequestLimit;
    });
    services.Configure<FormOptions>(x =>
    {
        x.ValueLengthLimit = maxRequestLimit;
        x.MultipartBodyLengthLimit = maxRequestLimit;
        x.MultipartHeadersLengthLimit = maxRequestLimit;
    });

and editing web.config:

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

Solution 4

I was using web.config to configure this (while our api is hosted in IIS):

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

But now we are moving our api to linux containers and using Kestrel. Then I configured it like this:

.ConfigureWebHostDefaults(webBuilder =>
{
    webBuilder
    .ConfigureKestrel(serverOptions =>
    {
        serverOptions.Limits.MaxRequestBodySize = 157286400;
    })
    .UseStartup<Startup>();
})

157286400 = 150mb;

Share:
17,664

Related videos on Youtube

mohammad rostami siahgeli
Author by

mohammad rostami siahgeli

Updated on June 21, 2022

Comments

  • mohammad rostami siahgeli
    mohammad rostami siahgeli almost 2 years

    I'm trying to upload a 100MB film to my ASP.NET Core application.

    I've set this attribute on my action:

    [RequestSizeLimit(1_000_000_000)]
    

    And I also changed my Web.config file to include:

    <security>
      <requestFiltering>
        <!-- This will handle requests up to 700MB (CD700) -->
        <requestLimits maxAllowedContentLength="737280000" />
      </requestFiltering>
    </security>
    

    In other words, I've told IIS to allow files up to 700MBs and I've also told ASP.NET Core to allow files of near 1GB.

    But I still get that error. And I can't find the answer. Any ideas?

    P.S: Using these configurations, I could pass the 30MB default size. I can upload files of 50 or 70 Mega Bytes.

  • Yor Jaggy
    Yor Jaggy almost 4 years
    I was close give up but your solution works like a charm! I also found this link github.com/dotnet/aspnetcore/issues/…
  • RoLYroLLs
    RoLYroLLs over 3 years
    This worked for me on a Brand new .Net Core 3.1 MVC app. other solutions, like updating the ConfigureServices on Startup.cs and @YorJaggy's url (which i found before this site), didn't work.