响应:413 请求实体太大

Jur*_*jen 12 c# asp.net-core aspnetboilerplate asp.net-core-webapi

当发布一个可以包含一个或多个文件(作为 base64 字符串)的请求时,我收到以下错误响应:

错误 2018-11-22 09:54:18,244 [13] Mvc.ExceptionHandling.AbpExceptionFilter - 远程服务器返回意外响应:(413)请求实体太大。System.ServiceModel.ProtocolException:远程服务器返回意外响应:(413) 请求实体太大。在 System.Runtime.AsyncResult.End[TAsyncResult](IAsyncResult result) 在 System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.End(SendAsyncResult result) 在 System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult 结果)在 System.ServiceModel.Channels.ServiceChannelProxy.TaskCreator.<>c__DisplayClass1_0.b__0(IAsyncResult asyncResult) --- 从上一个抛出异常的位置开始的堆栈跟踪结束 --- 在 ...

我已经搜索了如何解决这个问题,但我一直被重定向到 WCF 解决方案。

我已将以下内容添加到我的 WebApi 项目的 web.config 中,但它似乎没有什么区别。

<configuration>
  <system.webServer>
    ....
    <asp>
      <limits maxRequestEntityAllowed="2147483648"/>
    </asp>
    <serverRuntime uploadReadAheadSize="2147483647" />
  </system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮助我或为我指出正确的资源吗?

M. *_*ara 28

您需要更改两个限制。红隼和 IIS。

您可以在 Program.cs 中更改 Kestrel 的 MaxRequestBodySize 限制。

public static IWebHost BuildWebHost(string[] args)
{
    return WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .UseKestrel(options =>
        {
            options.Limits.MaxRequestBodySize = long.MaxValue;
        })
        .UseIISIntegration()
        .Build();
}
Run Code Online (Sandbox Code Playgroud)

并且可以在 web.config 中更改 IIS 的限制:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="2147483648" />
      </requestFiltering>
    </security>
  </system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)

  • 如果这不起作用怎么办?我在 .Net Core 3.1 API 中有一个像这样设置的 API,但我仍然遇到上述异常。 (2认同)
  • 谢谢!你的答案终于让它发挥作用了! (2认同)
  • @War .Net Core 3.0 及更高版本支持方法属性 [RequestSizeLimit()],因此您可以设置类似 [RequestSizeLimit(104857600)] 的内容以获得 100MB 的上传到该端点。 (2认同)

Tia*_*ila 14

对我来说,我必须在 web.config 设置中使用 maxAllowedContentLength 以及正在读取文件的操作的属性。我正在开发托管在 IIS 上的 .NET Core 3 应用程序。

ASP .Net Core 上的 Web.config 或 applicationHost.config:

<system.webServer>
    <security>
        <requestFiltering>
            <requestLimits maxAllowedContentLength="2147483648" />
        </requestFiltering>
    </security>
    ...
</system.webServer>
Run Code Online (Sandbox Code Playgroud)

找到applicationHost.config

<您的项目文件夹>/.vs/<您的项目名称>/config/applicationhost.config

如果还是不行,在Controller的Action上添加属性:

[DisableRequestSizeLimit]
[HttpPost]
public async Task<IActionResult> PostMyFile()
{
   ...
}
Run Code Online (Sandbox Code Playgroud)

  • 这仍然适用于 .NET 6 (3认同)