Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 457
mohammad rostami siahgeli Avatar asked Jul 03 '18 15:07

mohammad rostami siahgeli


2 Answers

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)
{ }
like image 82
DavB.cs Avatar answered Nov 17 '22 04:11

DavB.cs


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://learn.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-3.0#kestrel-maximum-request-body-size

like image 8
Ashish Rathi Avatar answered Nov 17 '22 04:11

Ashish Rathi