6

I would like to increase the default upload size of 30MB in ASP.NET Core 2.0 web app. The proposed solutions like adding [RequestSizeLimit(4_100_000_000)] or adding

services.Configure<FormOptions>(x => x.MultipartBodyLengthLimit = 4074790400);

do not work in asp.net core 2.0. I guess those solutions are for .NET Core 1+ versions.

I also added this into Program.cs, which is also proposed solution:

   public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .UseKestrel(options =>
            {
               options.Limits.MaxRequestBodySize = 4120100000;
            })
            .Build();

Although locally - using Visual studio docker debugger, I am able to upload files greater than 30 MB, in AWS environment I am getting again 413 Request Entity Too Large error.

Neno
  • 727
  • 3
  • 17
  • 34
  • 1
    Did you already take a look at this? https://stackoverflow.com/questions/46738364/increase-upload-request-length-limit-in-kestrel In .NET Core 2 it's `options.Limits.MaxRequestBodySize` in `UseKestrel`. – jAC Nov 05 '18 at 09:25
  • @jAC I added the UseKestrel part as I edited a few moments ago, but with no effect.. – Neno Nov 05 '18 at 13:22
  • 1
    Are you using IIS as a reverse proxy? If so, then you also need to add the request limit to your Web.config for it. In other words, your Core app is fine, but IIS is blocking it before it gets there. – Chris Pratt Nov 05 '18 at 14:08
  • @ChrisPratt, a good point! Actually it is dockerized web app and adding .UseKestrel(options => { options.Limits.MaxRequestBodySize = 4120100000; }) made it working in Visual studio docker environment, but not in AWS. – Neno Nov 05 '18 at 18:29
  • How are you running the Docker container? EKS, ECS,...? I'll try to replicate that behaviour tomorrow. – jAC Nov 05 '18 at 21:46
  • @jAC, it is running as EC2 instance. – Neno Nov 06 '18 at 09:59

2 Answers2

8

had the same issue on aws 18.04 ubuntu, running .net core 2.2 with nginx 1.16.1 as both front server and as reverse proxy.

fixed the issue by doing 2 things:

1) configuring the nginx's max request size - to make sure that the front nginx isn't blocking the request from ever reaching the app behind the reverse proxy. to do that i've added 'adding client_max_body_size 30M; ' inside the server block, but there are more ways of doing this, see: https://www.tecmint.com/limit-file-upload-size-in-nginx/)

2) adding the same code to the Main, with 1 small variation - instead of specifying the request size, I've inserted a null value, which causes kestrel to assume that it's the largest possible value (afaik)

 public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .UseKestrel(options =>
            {
               options.Limits.MaxRequestBodySize = null;
            })
            .Build();
nir weiner
  • 305
  • 3
  • 10
0

If you are using Visual Studio on Windows with IIS Express, no need to use Kestrel.

Make a change in your applicationHost.config or Web.config file:

  • Open applicationHost.config under <YourProjectFolder>/.vs/<YourProjectName>/config/applicationhost.config or Open your Web.config file.
  • Find <system.webserver> containing <security> that contains <requestFiltering>.
  • Add this <requestLimits maxAllowedContentLength="2147483648" />

Hence the final code will look like:

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

If this does not work, try adding [DisableRequestSizeLimit] attribute in your Post action in ASP .Net Core Project's Controller.

[HttpPost("postfiles")]
[DisableRequestSizeLimit]
public async Task<ActionResult> PostFiles()
{
    ...
}
Junaid Pathan
  • 3,850
  • 1
  • 25
  • 47