【问题标题】:HTTP Error 404.13 - asp.net core 2.0HTTP 错误 404.13 - asp.net 核心 2.0
【发布时间】:2018-03-12 15:47:51
【问题描述】:

HTTP 错误 404.13 - Not Found 请求过滤模块是 配置为拒绝超过请求内容长度的请求。

验证 配置/system.webServer/security/requestFiltering/requestLimits@maxAllowedContentLength 在 applicationhost.config 或 web.config 文件中设置。

我不知道在哪里可以配置,在 asp.net core 2 中有更改为使用 appsettings.json。

即使已经尝试这样做,但它不起作用。

services.Configure<FormOptions>(options =>
{
    options.MultipartBodyLengthLimit = 300_000_000;
});

【问题讨论】:

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


【解决方案1】:

如果您使用 IIS,则需要在您的 asp.net core 2.0 应用中添加 web.config。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <security>
      <requestFiltering>
        <!-- This will handle requests up to 700MB (CD700) -->
        <requestLimits maxAllowedContentLength="737280000" />
      </requestFiltering>
    </security>
  </system.webServer>
</configuration>

如果您不使用 IIS,您可以在控制器中使用 [RequestSizeLimit(long.MaxValue)][DisableRequestSizeLimit] 属性。

另外,您可以在 Program.cs 中添加全局设置:

.UseKestrel(o => { o.Limits.MaxRequestBodySize = null; })

欲了解更多信息,请参阅https://github.com/aspnet/Announcements/issues/267

【讨论】:

  • UseKestrel 已被弃用且未在以后的 .NET Core 版本中使用。
【解决方案2】:

对于我的 Core 2.2 MVC 项目,结合 [RequestSizeLimit(long.MaxValue)] 属性和上面的 web.config 工作。 web.config 单独工作 - 只是不要使用该属性。应用程序崩溃并意外关闭浏览器。另外,因为我的最大请求大小是 200Mb,如果可以生成 404.13(请求太大)结果,那么您的正常状态代码处理将适用于 404.13(请参阅 Startup.cs,配置(),

app.UseStatusCodePagesWithReExecute("/Error/Error", "?statusCode={0}");

行)。状态代码页处理虽然适用于其他代码。我必须将自定义错误状态处理插入到 web.config 文件中,以便以优雅的用户友好视图处理 404.13。请注意,404.13 的“删除”似乎也删除了状态代码 404 ...然后您的错误控制器方法将无法处理 404。下面的 web.config 中有两个自定义错误处理程序 - 一个用于 404.13,一个用于为 404 以解决此问题。希望这可以帮助某人:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <security>
      <requestFiltering>
        <!-- This will handle requests up to 201Mb -->
        <requestLimits maxAllowedContentLength="210763776" />
      </requestFiltering>
    </security>
    <httpErrors errorMode="Custom" existingResponse="Replace">
      <remove statusCode="404" subStatusCode="13" />
      <remove statusCode="404" />
      <error statusCode="404"
             subStatusCode="13"
             prefixLanguageFilePath=""
             path="/Error/UploadTooLarge"
             responseMode="Redirect" />
      <error statusCode="404"
             prefixLanguageFilePath=""
             path="/Error/PageNotFound"
             responseMode="Redirect" />
    </httpErrors>
  </system.webServer>
</configuration>

最终的结果是所有正常的状态码都被Error/Error处理了,404.13和404状态码都有自定义处理......而且周围的景色都很好!

【讨论】:

    【解决方案3】:

    内容长度的默认值 30000000 字节,在某些文件上传功能中对我来说还不够。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-03
      • 2021-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-24
      相关资源
      最近更新 更多