5

I have an older ASP.NET API web service using .net 4.5.2 and I am posting an object that contains a base64 image to my controller without any problem. The issues come now when I try to post data with more and larger images and I am getting the 413 request entity too large error. I have been looking up things and tried everything I could find on the net with no luck. I am looking to upload files about 10MB in size. One thing that leads me to believe its server related is when running the service under IIS Express I can upload large files locally.

I have tried adding MaxRequestLength and MaxAllowableContentLength to the web config.

<system.web>
    <!-- tell IIS to let large requests through -->
    <httpRuntime maxRequestLength="52428800" />

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

I have also made a change on my windows 2012 R2 server in IIS v6.2 to allow larger files. I have also adjusted the UploadReadAhead value on the server.

enter image description here

enter image description here

enter image description here

I don't have anything special in API config class.

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
        config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
    }
}

The bindings in my config for this service

    <webHttpBinding>
        <binding name="SecureServerBindingWithUserNameRest"  maxReceivedMessageSize="52428800">
            <readerQuotas maxArrayLength="10485760" />
                <security mode="TransportCredentialOnly">
                <transport clientCredentialType="Basic" proxyCredentialType="None" />
            </security>
        </binding>
        <binding name="webHttpTransportSecurityRest">
            <security mode="Transport" />
        </binding>
    </webHttpBinding>
scott lafoy
  • 1,001
  • 1
  • 13
  • 30
  • maybe in web.config look at https://stackoverflow.com/questions/1151987/can-i-set-an-unlimited-length-for-maxjsonlength-in-web-config – Carlos Cocom Aug 22 '22 at 17:15
  • I had a look at the tagged SO and tried a few things, no luck. – scott lafoy Aug 24 '22 at 22:53
  • Have you tried this? https://stackoverflow.com/a/58566617/3310091 You can configure at the `startup.cs` with `services.Configure` – Camadas Aug 31 '22 at 09:40
  • The below URL can help you [link](https://stackoverflow.com/a/26725542/12241692) – krishna chaitanya Sep 01 '22 at 09:05
  • This could also be an error thrown by a load balancer (for example a Nginx) that seats in front of the web server. If there's a Nginx in front, the admin needs to increase the value of the property client_max_body_size. – Cristian Rusanu Sep 01 '22 at 14:52
  • can you check this https://stackoverflow.com/questions/288612/how-to-increase-the-max-upload-file-size-in-asp-net – Carlo Luisito Sep 06 '22 at 01:23

3 Answers3

2

You can try this solution, try to increase the upload size limit. IIS uses uploadReadAheadSize parameter in applicationHost.config and web.config files.

  1. Open IIS Manager
  2. Select the site
  3. Double click "Configuration Editor"
  4. Select system.webServer and then serverRuntime
  5. Modify the uploadReadAheadSize value to 2147483647
  6. Click "Apply"
samwu
  • 3,857
  • 3
  • 11
  • 25
  • Thanks for the suggestion, I tried this but its still refusing with the request entity too large error – scott lafoy Aug 24 '22 at 22:11
  • Can you share what kind of bindings you've configured in the web.config? – samwu Aug 25 '22 at 09:44
  • I added it to the end of the question. – scott lafoy Aug 26 '22 at 21:28
  • You can also try to change `maxRequestEntityAllowed` parameter, It specifies the maximum number of bytes allowed in the request body. – samwu Aug 30 '22 at 09:11
  • In the last picture on my question I have MaxRequestEntityAllowed set, it is the one right above uploadReadAheadSize . Is this the one you are referring to? – scott lafoy Aug 30 '22 at 18:22
  • It is difficult to reproduce your problem, I suggest you open a case via: https://support.microsoft.com. – samwu Aug 31 '22 at 10:05
1

When I encountered this error while sendig zipped files containing a large amount of PDFS, the solution I found was to also add to the web config requestLengthDiskThreshold apart from the maxRequestLength:

<system.web>
    <httpRuntime maxRequestLength="268435456" requestLengthDiskThreshold="268435456" executionTimeout="600"/>

I also added a longer timeout than the 90 preset so it had time to send the file. Just have in mind that the requestLengthDiskThreshold must not exceed the maxRequestLength.

Alan Warden
  • 188
  • 8
0

You might also consider executing a dummy get request just before posting large content. When a https connection is initialized, this first request should not be very large. Once the https connection is initialized proceeding requests are not limitted.

rekna
  • 5,313
  • 7
  • 45
  • 54