ASP.NET Core web.config requestFiltering 不覆盖 applicationhost.config

Xer*_*lio 5 c# asp.net-core-mvc asp.net-core

我正在尝试将大文件上传到我的 ASP.NET Core MVC 2.1 应用程序中的 API 控制器操作。为此,我一直试图弄清楚如何通过 IIS Express 允许这样做,这就是我通过 Visual Studio 运行应用程序的方式。正如所建议的,这应该可以通过向web.config项目根目录添加一个包含以下内容的文件来实现:

<?xml version="1.0" encoding="utf-8"?>
<!-- Configuration for IIS integration -->
<configuration>
  <system.webServer>
    <security>
      <requestFiltering>
        <!-- 2 GB -->
        <requestLimits maxAllowedContentLength="2147483647" />
      </requestFiltering>
    </security>
  </system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)

但是,这没有任何影响,因为应用程序只返回HTTP Error 404.13 - Not Found,表明请求太大。似乎这些设置被 IIS 锁定,因此web.config不会覆盖它们。是的,我也在控制器操作上使用[DisableFormValueModelBinding]和使用属性[DisableRequestSizeLimit]

相反,我发现,它的工作原理是将相同的配置,该网站在applicationhost.config\.vs\config的文件夹:

  <location path="MySite">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" startupTimeLimit="3600" requestTimeout="23:00:00" />
      <httpCompression>
        <dynamicTypes>
          <add mimeType="text/event-stream" enabled="false" />
        </dynamicTypes>
      </httpCompression>
      <!-- Everything above this point is auto-generated -->
      <security>
        <requestFiltering>
          <!-- 2 GB -->
          <requestLimits maxAllowedContentLength="2147483647" />
        </requestFiltering>
      </security>
    </system.webServer>
  </location>
Run Code Online (Sandbox Code Playgroud)

但是,此文件未在 GIT 中跟踪,将其添加到 GIT 似乎也不是一个好的解决方案。

是否有一些安全原因或其他原因web.config似乎不允许覆盖applicationhost.config?或者是否有我一直无法弄清楚的解决方案?

yes*_*can 1

我有一个类似的问题,我添加了一个 web.config

<configuration>
  <system.webServer>
    <security>
      <requestFiltering>
        <!-- This will handle requests up to 700MB (CD700) -->
        <requestLimits maxAllowedContentLength="737280000" />
      </requestFiltering>
    </security>
  </system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)

发布得太早了。上述解决方案适用于 iis Express 服务器,但不适用于 docker。然后我更新了条目

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .UseKestrel(options =>
        {
            options.Limits.MaxRequestBodySize = 100000000; //100MB
        });
Run Code Online (Sandbox Code Playgroud)

然后用以下内容装饰控制器 [RequestSizeLimit(100_000_000)]

这些解决方案在本地和产品中为我解决了。