首页 > 解决方案 > 如何增加 .net core 中的请求限制?

问题描述

我正在尝试在我的 .net 核心 Web api 中上传一些大于 ~30Mb 限制的文件。我已经尝试遵循我找到的所有建议,所有建议都在下面列出。无论我尝试过什么,都会返回 404.13 错误“请求超出请求内容长度”。我在 Windows 10 上的 Visual Studio 中运行,有人知道我怎样才能让它工作吗?我猜在 IIS 上托管时情况可能会有所不同,但我试图首先在我的机器上运行它。

public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseKestrel(o => o.Limits.MaxRequestBodySize = null)
            .UseStartup<Startup>()
            .Build();

下面的这个在尝试获取 IHttpMaxRequestBodySizeFeature 时返回 null

appBuilder.ServerFeatures.Get<IHttpMaxRequestBodySizeFeature>.MaxRequestBodySize = null;

将其添加到我的 app.config 中:

    <?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <runtime>
    <gcServer enabled="true"/>
  </runtime>
  <system.web>
    <httpRuntime maxRequestLength="2147483647"/>
  </system.web>
  <system.webServer>

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

向我的控制器添加属性:

[HttpPost]
    [DisableRequestSizeLimit]
    //[RequestSizeLimit(100_000_000)]
    public IActionResult Document(IList<IFormFile> files)

标签: asp.net-web-apiasp.net-corevisual-studio-2017

解决方案


为了回答我自己的问题,事实证明我应该创建一个 Web.config 文件并将我放在 app.config 文件中的内容放入其中。


推荐阅读