.net - Kestrel MaxRequestBodySize 上传文件超限

标签 .net asp.net-core kestrel-http-server

我确实遇到了红隼的一个奇怪问题。我无法上传超过红隼 MaxRequestBodySize 的多个文件。
当我试图阅读 this.Request.Form.Files.GetFiles() 时,预期的行为是抛出 BadHttpRequestException。我确实希望只收到一次对 Controller 操作的请求。
发生的事情是上传操作被点击了几次,浏览器显示消息“连接丢失”。我没有找到关于 Action 被调用的时间的模式。
Controller Action :

[HttpPost("upload")]
public IActionResult Upload()
{
    try
    {
        var files = this.Request.Form.Files.GetFiles("files");
        files.Select(async file => await this.SaveFile(file))
        return this.RedirectToAction(nameof(VueController.FilesList),"Vue");
    }
    catch (BadHttpRequestException exp)
    {
        return new string[]
        {
            exp.Message
        };
    }
}
看法:
<form method="post"
          enctype="multipart/form-data"
          action="/api/v1/files/upload"
          novalidate="novalidate">
      <input type="file"
             name="files"
             multiple="multiple"
             required="required"
             accept=""
             capture="capture" />
    </form>
asp.net 核心日志:

info: Microsoft.AspNetCore.Server.Kestrel[17] Connection id "0HLDB9K94VV9M" bad request data: "Request body too large." Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException: Request body too large. at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Frame.ThrowRequestRejected(RequestRejectionReason reason) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.MessageBody.ForContentLength.OnReadStart() at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.MessageBody.TryInit() at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.MessageBody.d__24.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Frame`1.d__2.MoveNext() info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2] Request finished in 7618.6319ms 413


已编辑
我知道我可以禁用限制,但在这种情况下是不可能的。

最佳答案

您必须配置两件事:

在您的 Program.cs

public static IWebHost BuildWebhost(string[] args) => 
   WebHost.CreateDefaultBuilder(args)
      .UseStartup<Startup>()
      .UseKestrel(options => {
           options.Limits.MaxRequestBodySize = null; // or a given limit
      })
     .Build();

在 ConfigureService 方法中的 Startup.cs
services.Configure<FormOptions>(options => options.MultipartBodyLengthLimit = long.MaxValue); // or other given limit

还要更改您的 Controller 端点以使用 [FromForm]
public IActionResult Upload([FromForm] IEnumerable<IFormFile> files)... // name must be same as name attribute of your multipart form

现在 ASP.NET Core 将完成这项工作并将表单中的文件作为序列注入(inject)。

编辑:

我创建了一个可以从 github 克隆的示例:
git clone https://github.com/alsami/example-fileupload-aspnet-core.git
cd example-fileupload-aspnet-core
dotnet restore
dotnet run --project src/file-upload-api/file-upload-api.csproj

然后导航到 http://localhost:5000/index.html并尝试上传大文件。

关于.net - Kestrel MaxRequestBodySize 上传文件超限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50037706/

相关文章:

multithreading - 如何在 Linux (Kubernetes) 上解决 ASP.NET Core 中的线程不足问题?

.net - 不会在表单加载时触发的 SelectedIndexChanged 替代方案?

.net - 回调方法 C#

c# - 使用 LINQ 根据字段过滤 JSON 对象

c# - Entity Framework IdentityUser覆盖用户名不会保存在数据库中

c# - .net core 版本冲突

.net - 如何开始对大型应用程序进行自动化测试?

c# - AuthenticationBuilder 不包含 AddAzureAd 的定义

asp.net-core - Kestrel 失败 : Can not load Microsoft. Extensions.Logging.Abstractions

c# - ECONNRESET 连接由对等 Kestrel 重置