HTTP 错误 404.13 - asp.net 核心 2.0

HTTP Error 404.13 - asp.net core 2.0

HTTP Error 404.13 - Not Found The request filtering module is configured to deny a request that exceeds the request content length.

Verify the configuration/system.webServer/security/requestFiltering/requestLimits@maxAllowedContentLength setting in the applicationhost.config or web.config file.

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

甚至已经尝试过这样做,但没有用。

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

如果您使用 IIS,您需要在 asp.net 核心 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

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

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

行)。不过,状态代码页处理适用于其他代码。我必须将自定义错误状态处理插入到 web.config 文件中,以使用优雅的用户友好视图处理 404.13。请注意,404.13 的 'remove' 似乎也删除了状态代码 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 状态代码有自定义处理......和周围的美景!

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

<system.webServer>
    <security>

      <requestFiltering>
        <requestLimits maxAllowedContentLength="500000000"></requestLimits>

      </requestFiltering>
    </security>

  </system.webServer>