简体   繁体   中英

ASP.NET web service 413 request entity too large error

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.

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

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>

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"

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM