【问题标题】:Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException: Request body too largeMicrosoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException:请求正文太大
【发布时间】:2018-07-03 15:20:10
【问题描述】:

我正在尝试将 100MB 影片上传到我的 ASP.NET Core 应用程序。

我已经在我的操作中设置了这个属性:

[RequestSizeLimit(1_000_000_000)]

我还更改了我的 Web.config 文件以包括:

<security>
  <requestFiltering>
    <!-- This will handle requests up to 700MB (CD700) -->
    <requestLimits maxAllowedContentLength="737280000" />
  </requestFiltering>
</security>

换句话说,我告诉 IIS 允许最大 700MB 的文件,我还告诉 ASP.NET Core 允许接近 1GB 的文件。

但我仍然收到该错误。我找不到答案。有什么想法吗?

P.S:使用这些配置,我可以通过 30MB 的默认大小。我可以上传 50 或 70 兆字节的文件。

【问题讨论】:

标签: iis asp.net-core asp.net-core-mvc


【解决方案1】:

我想你只需要:[DisableRequestSizeLimit]

下面是一个解决方案,可让我将带有额外表单数据的 Zip 文件上传到运行 .Net Core 3 的 API

// MultipartBodyLengthLimit  was needed for zip files with form data.
// [DisableRequestSizeLimit] works for the KESTREL server, but not IIS server 
// for IIS: webconfig... <requestLimits maxAllowedContentLength="102428800" />
[RequestFormLimits(ValueLengthLimit = int.MaxValue, MultipartBodyLengthLimit = int.MaxValue)] 
[DisableRequestSizeLimit] 
[Consumes("multipart/form-data")] // for Zip files with form data
[HttpPost("MyCustomRoute")]
public IActionResult UploadZippedFiles([FromForm] MyCustomFormObject formData)
{ }

【讨论】:

  • 我差点放弃,但你的解决方案就像一个魅力!我还找到了这个链接github.com/dotnet/aspnetcore/issues/…
  • 这对我来说适用于全新的 .Net Core 3.1 MVC 应用程序。其他解决方案,例如更新Startup.cs 上的ConfigureServices 和@YorJaggy 的网址(我在此站点之前找到的)都不起作用。
【解决方案2】:

注意:这是我从迁移应用程序时遇到的问题 asp.net core 2.1 到 3.0

为了在 asp.net core 3.0 中解决这个问题,我更改了我的 program.cs 以修改最大请求正文大小,如下所示。

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

    public static IWebHostBuilder CreateWebHostBuilder(string[] args)
    {
       return WebHost.CreateDefaultBuilder(args)
            .ConfigureKestrel((context, options) =>
            {
                options.Limits.MaxRequestBodySize = 737280000;
            })
            .UseStartup<Startup>();
        }
    }
}

我的意思是我刚刚添加了ConfigureKestrel 部分并在我的操作方法[RequestSizeLimit(737280000)] 上方添加了一个属性,如下所示

[HttpPost]
[RequestSizeLimit(737280000)]
[Route("SomeRoute")]
public async Task<ViewResult> MyActionMethodAsync([FromForm]MyViewModel myViewModel)
{
   //Some code
   return View();
}

我的应用程序再次开始正常运行而没有抛出BadHttpRequestException: Request body too large

参考:https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-3.0#kestrel-maximum-request-body-size

【讨论】:

    【解决方案3】:

    对我来说(Asp.net core 3.1),解决方案是在Startup.csConfigureServices 方法中添加这些行:

        // 200 MB
        const int maxRequestLimit = 209715200;
        // If using IIS
        services.Configure<IISServerOptions>(options =>
        {
            options.MaxRequestBodySize = maxRequestLimit;
        });
        // If using Kestrel
        services.Configure<KestrelServerOptions>(options =>
        {
            options.Limits.MaxRequestBodySize = maxRequestLimit;
        });
        services.Configure<FormOptions>(x =>
        {
            x.ValueLengthLimit = maxRequestLimit;
            x.MultipartBodyLengthLimit = maxRequestLimit;
            x.MultipartHeadersLengthLimit = maxRequestLimit;
        });
    

    和编辑web.config:

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

    【讨论】:

      【解决方案4】:

      我使用web.config 进行配置(而我们的 api 托管在 IIS 中):

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

      但是现在我们正在将我们的 api 移动到 linux 容器并使用 Kestrel。然后我是这样配置的:

      .ConfigureWebHostDefaults(webBuilder =>
      {
          webBuilder
          .ConfigureKestrel(serverOptions =>
          {
              serverOptions.Limits.MaxRequestBodySize = 157286400;
          })
          .UseStartup<Startup>();
      })
      

      157286400 = 150mb;

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-30
        • 1970-01-01
        • 1970-01-01
        • 2019-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多