40

I am running an ASP.NET Core web app and want to upload large files.

I know that when running IIS, the limits can be changed via web.config:

<httpRuntime maxRequestLength="1048576" /> 
...
<requestLimits maxAllowedContentLength="1073741824" /> 

How can you do the equivalent while running the new ASP.NET Core Kestrel web server?

I get the exception "Request body too large."

poke
  • 369,085
  • 72
  • 557
  • 602
ToddBFisher
  • 11,370
  • 8
  • 38
  • 54

2 Answers2

86

I found this helpful announcement that confirms there is a 28.6 MB body size limit starting with ASP.NET Core 2.0, but more importantly shows how to get around it!

To summarize:

For a single controller or action, use the [DisableRequestSizeLimit] attribute to have no limit, or the [RequestSizeLimit(100_000_000)] to specify a custom limit.

To change it globally, inside of the BuildWebHost() method, inside the Program.cs file, add the .UseKestrel option below:

WebHost.CreateDefaultBuilder(args)
  .UseStartup<Startup>()
  .UseKestrel(options =>
  {
    options.Limits.MaxRequestBodySize = null;
  }

For additional clarity, you can also refer to the Kestrel options documentation.

poke
  • 369,085
  • 72
  • 557
  • 602
ToddBFisher
  • 11,370
  • 8
  • 38
  • 54
  • 3
    Sorry, it feels weird to answer my own question, but I hope others will benefit from this. – ToddBFisher Oct 13 '17 at 22:26
  • 12
    Answering your own question is [perfectly fine](https://stackoverflow.com/help/self-answer) and you are encouraged to do so any time if you have a question that you believe is useful! So thanks for researching! :) – poke Oct 13 '17 at 22:38
  • Hey! can you tell what's the max Request header size on Kestrel? – Zeeshan Adil Mar 20 '19 at 12:55
  • I am not aware of any absolute limitations other than that of the corresponding data type's max value (`Int32.MaxValue`) as found on the configurable `KestrelServerLimits.MaxRequestHeadersTotalSize` property. You could try asking a new question on SO. – ToddBFisher Mar 20 '19 at 18:57
6

The other answer is for ASP.NET Core 2.0, but I would like to provide the solution for .NET Core 3.x web API.

Your code in program.cs must be like this to work:

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

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
                webBuilder.UseKestrel(options =>
                {
                    options.Limits.MaxRequestBodySize = null;
                });
            });
}
phoenix
  • 7,988
  • 6
  • 39
  • 45
Akbar Asghari
  • 613
  • 8
  • 19