【问题标题】:Increase upload request length limit in Kestrel增加 Kestrel 中的上传请求长度限制
【发布时间】:2018-03-26 02:07:44
【问题描述】:

我正在运行一个 ASP.NET Core Web 应用程序并想上传大文件。

我知道在运行 IIS 时,可以通过web.config 更改限制:

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

在运行新的 ASP.NET Core Kestrel Web 服务器时如何做同样的事情?

我收到异常“请求正文太大。”

【问题讨论】:

    标签: file-upload asp.net-core asp.net-core-2.0 kestrel-http-server


    【解决方案1】:

    我发现this helpful announcement 确认从 ASP.NET Core 2.0 开始有 28.6 MB 的正文大小限制,但更重要的是展示了如何绕过它!

    总结一下:

    对于单个控制器或操作,使用[DisableRequestSizeLimit] 属性没有限制,或使用[RequestSizeLimit(100_000_000)] 指定自定义限制。

    要全局更改它,在 BuildWebHost() 方法内,在 Program.cs 文件内,在下面添加 .UseKestrel 选项:

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

    为了更清楚,您还可以参考Kestrel options documentation

    【讨论】:

    • 对不起,回答我自己的问题感觉很奇怪,但我希望其他人能从中受益。
    • 回答您自己的问题是perfectly fine,如果您有任何您认为有用的问题,我们鼓励您随时回答!所以感谢您的研究! :)
    • 嘿!你能说出 Kestrel 上的最大请求标头大小是多少吗?
    • 我不知道除了在可配置的KestrelServerLimits.MaxRequestHeadersTotalSize 属性上找到的相应数据类型的最大值 (Int32.MaxValue) 之外的任何绝对限制。您可以尝试在 SO 上提出一个新问题。
    【解决方案2】:

    other answer 适用于 ASP.NET Core 2.0,但我想为 .NET Core 3.x Web API 提供解决方案。

    program.cs 中的代码必须是这样才能工作:

    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;
                    });
                });
    }
    

    【讨论】:

      猜你喜欢
      • 2021-03-17
      • 2010-10-15
      • 2020-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-23
      相关资源
      最近更新 更多