【问题标题】:ASP.NET Core web.config requestFiltering not overriding applicationhost.configASP.NET Core web.config requestFiltering 不覆盖 applicationhost.config
【发布时间】:2020-01-23 22:45:26
【问题描述】:

我正在尝试将大文件上传到我的 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>

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

相反,我发现它可以通过在applicationhost.config 文件夹中的applicationhost.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>

然而,这个文件在 GIT 中没有被跟踪,而且将它添加到 GIT 似乎也不是一个好的解决方案。

为什么web.config 似乎不允许覆盖applicationhost.config,是否有一些安全原因或其他原因?或者有没有我想不通的解决方案?

【问题讨论】:

  • 当它一定是一个非常普遍的问题时,不知道为什么没有答案。唉,我最终恢复到只使用 POST 而不是 GET,我想这更有意义。

标签: c# asp.net-core asp.net-core-mvc


【解决方案1】:

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

<configuration>
  <system.webServer>
    <security>
      <requestFiltering>
        <!-- This will handle requests up to 700MB (CD700) -->
        <requestLimits maxAllowedContentLength="737280000" />
      </requestFiltering>
    </security>
  </system.webServer>][1]][1]
</configuration>

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

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

然后用[RequestSizeLimit(100_000_000)]装饰控制器

这些解决方案在本地解决并为我生产。

【讨论】:

  • 感谢您的反馈,但它似乎与我发布的配置没有任何不同,只是数字较低,这无法解决我的问题。另外,当您在 SO 上提交代码时的一个建议:请避免使用图片 - 我无法真正复制粘贴。
  • 解决方案更新希望它能解决你的问题;)祝你好运
猜你喜欢
  • 1970-01-01
  • 2016-11-19
  • 2013-10-15
  • 2017-09-30
  • 1970-01-01
  • 2012-08-07
  • 1970-01-01
  • 1970-01-01
  • 2018-04-04
相关资源
最近更新 更多