26

When POSTing a request which can contain one or more files (as base64 string) I get this error response:

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 ...

I have searched on how to resolve this but I get redirected to WCF solutions all the time.

I have added the following to the web.config of my WebApi project but it doesn't seem to make a difference.

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

Can anyone help me or point me to the right resource?

M. Mennan Kara
  • 10,072
  • 2
  • 35
  • 39
Jurjen
  • 764
  • 1
  • 10
  • 24
  • For .Net Core 3.1 have a look to this answer: https://stackoverflow.com/questions/60675202/how-to-migrate-asp-net-framework-configurations-to-asp-net-core – m2v Jul 22 '20 at 22:23

3 Answers3

53

There are two limits you need to change. Kestrel and IIS.

You can change the MaxRequestBodySize limit of Kestrel in Program.cs.

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

And the limit for IIS can be changed in web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="2147483648" />
      </requestFiltering>
    </security>
  </system.webServer>
</configuration>
M. Mennan Kara
  • 10,072
  • 2
  • 35
  • 39
  • 2
    What if this doesn't work? I have an API in a .Net Core 3.1 API that's setup like this and i'm still getting the above exception. – War Jul 21 '20 at 08:11
  • 2
    thanks! your anwser finally make it work! – Carl Verret Mar 16 '21 at 19:34
  • 5
    @War .Net Core 3.0 and above supports the method attribute [RequestSizeLimit()] so you can set something like [RequestSizeLimit(104857600)] to get 100MB uploads to that endpoint. – Steve Hiner Apr 22 '21 at 22:09
13

For me I had to do in the web.config setting using maxAllowedContentLength and also an attribute on the action that was reading the file. I'm working on a .NET Core 3 application hosted on IIS.

Web.config or applicationHost.config on ASP .Net Core:

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

Locate applicationHost.config under

<YourProjectFolder>/.vs/<YourProjectName>/config/applicationhost.config

If it still does not work, add attribute on the Action of the Controller:

[DisableRequestSizeLimit]
[HttpPost]
public async Task<IActionResult> PostMyFile()
{
   ...
}
Junaid Pathan
  • 3,850
  • 1
  • 25
  • 47
Tiago Ávila
  • 2,737
  • 1
  • 31
  • 34
1

I think, you may need to modify your web.config then add "maxRequestLength" and "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>
Lemons
  • 107
  • 1
  • 13
  • 2
    I already tried setting maxAllowedContentLength, it doesn't make a difference. I'm using .NET Core, so I'm not sure System.Web will work, specially referencing 4.5.2 as targetFramework. – Jurjen Nov 22 '18 at 11:13