响应:413 请求实体太大

response: 413 Request Entity Too Large

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

ERROR 2018-11-22 09:54:18,244 [13 ] Mvc.ExceptionHandling.AbpExceptionFilter - The remote server returned an unexpected response: (413) Request Entity Too Large. System.ServiceModel.ProtocolException: The remote server returned an unexpected response: (413) Request Entity Too Large. at System.Runtime.AsyncResult.End[TAsyncResult](IAsyncResult result) at System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.End(SendAsyncResult result) at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result) at System.ServiceModel.Channels.ServiceChannelProxy.TaskCreator.<>c__DisplayClass1_0.b__0(IAsyncResult asyncResult) --- End of stack trace from previous location where exception was thrown --- at ...

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

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

<configuration>
  <system.webServer>
    ....
    <asp>
      <limits maxRequestEntityAllowed="2147483648"/>
    </asp>
    <serverRuntime uploadReadAheadSize="2147483647" />
  </system.webServer>
</configuration>

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

您需要更改两个限制。 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>

我认为,您可能需要修改 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>

对我来说,我必须在 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(){
   ...
}