Increase upload request length limit in Kestrel

前端 未结 2 1035
说谎
说谎 2020-12-09 07:50

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:



        
相关标签:
2条回答
  • 2020-12-09 08:16

    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.

    0 讨论(0)
  • 2020-12-09 08:20

    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;
                    });
                });
    }
    
    0 讨论(0)
提交回复
热议问题