【问题标题】:response: 413 Request Entity Too Large响应:413 请求实体太大
【发布时间】:2019-04-24 23:47:05
【问题描述】:

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

ERROR 2018-11-22 09:54:18,244 [13] Mvc.ExceptionHandling.AbpExceptionFilter - 远程服务器返回意外响应:(413) 请求实体太大。 System.ServiceModel.ProtocolException:远程服务器返回意外响应:(413)请求实体太大。 在 System.Runtime.AsyncResult.End[TAsyncResult](IAsyncResult 结果) 在 System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.End(SendAsyncResult 结果) 在 System.ServiceModel.Channels.ServiceChannel.EndCall(字符串操作,对象 [] 输出,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>

谁能帮助我或为我指出正确的资源?

【问题讨论】:

标签: c# asp.net-core asp.net-core-webapi aspnetboilerplate


【解决方案1】:

您需要更改两个限制。 Kestrel 和 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();
}

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

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="2147483648" />
      </requestFiltering>
    </security>
  </system.webServer>
</configuration>

【讨论】:

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

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

Web.config:

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

控制器动作的属性:

[DisableRequestSizeLimit]
[HttpPost]
public async Task<IActionResult> PostMyFile(){
   ...
}

【讨论】:

    【解决方案3】:

    我认为,您可能需要修改您的 web.config,然后添加“maxRequestLength”和“maxAllowedContentLength”

    <system.web>
        <compilation debug="true" targetFramework="4.5.2"/>
        <httpRuntime targetFramework="4.5.2" maxRequestLength="50240" executionTimeout="600"/> <!-- in Kb  -->
      </system.web>
      <system.webServer>
        <security>
          <requestFiltering>
             <requestLimits maxAllowedContentLength="52428800" /> <!-- in byte (50 Mb) -->                                               
          </requestFiltering>
       </security>
    

    【讨论】:

    • 我已经尝试设置 maxAllowedContentLength,它没有任何区别。我正在使用 .NET Core,所以我不确定 System.Web 是否可以正常工作,特别是引用 4.5.2 作为 targetFramework。
    猜你喜欢
    • 2014-12-30
    • 2014-12-23
    • 1970-01-01
    • 2012-09-23
    • 2015-10-04
    • 2015-01-25
    • 2014-12-04
    • 2017-08-28
    相关资源
    最近更新 更多