HTTP 错误 404.13 - asp.net core 2.0

12
HTTP错误404.13 - 未找到 请求筛选模块配置拒绝超出请求内容长度限制的请求。请检查应用程序host.config或web.config文件中的configuration/system.webServer/security/requestFiltering/requestLimits@maxAllowedContentLength设置。在asp.net core 2中,可以使用appsettings.json进行配置,但我尝试过了,但仍然无法解决问题。
services.Configure<FormOptions>(options =>
{
    options.MultipartBodyLengthLimit = 300_000_000;
});

Asp.net core可以有一个web.config文件,如果你在IIS上托管它。另请阅读:https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration?tabs=basicconfiguration#the-webconfig-file。 - Styxxy
3个回答

37

如果您使用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版本中使用。 - fletchsod

0
对于我的Core 2.2 MVC项目,结合 [RequestSizeLimit(long.MaxValue)] 属性和上面的 web.config 并没有 起作用。只有 web.config 单独起作用 - 不要使用属性。应用程序崩溃并意外关闭浏览器。另外,由于我最大的请求大小是200MB,如果可以生成404.13(请求太大)结果,那么你的正常状态码处理将不会针对404.13工作(请参阅Startup.cs、Configure())。
app.UseStatusCodePagesWithReExecute("/Error/Error", "?statusCode={0}");

这段代码中的状态码页面处理对于其他代码是有效的。但是,我不得不在web.config文件中插入自定义错误状态处理来处理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状态码具有自定义处理...并且周围都是漂亮的视图!

0

在某些文件上传功能中,30000000字节的默认内容长度不足以满足我的需求。

<system.webServer>
    <security>

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

      </requestFiltering>
    </security>

  </system.webServer>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接