[ACCEPTED]-HTTP 413 - Request Entity Too Large cheat sheet-.net-core

Accepted answer
Score: 11

I created an asp.net mvc application and 11 tried to test it.

Often we only pay attention 10 to the uploadReadAheadSize setting, but tests have proved that 9 this cannot completely solve the problem, and 8 there are other configurations that need 7 to be configured. (Added based on Jack's 6 answer)

In addition to setting the uploadReadAheadSize value 5 larger, there are also maxAllowedContentLength and maxRequestLength. You need to 4 modify all these three values, otherwise 3 the 413 or Maximum request length exceeded error will still be displayed. enter image description here enter image description here enter image description here

In 2 my test results, the application can upload 1 any type of file within 1GB.

Score: 1

Here is my cheat sheet:

The max file size 34 limit coming through from IIS is specified 33 through the UploadReadAhaead parameter, which 32 can be specified in web.config as so. In 31 this case, the maximum is set to 1000485760 30 bytes

<?xml version="1.0" encoding="utf-8"?> 
    <httpErrors errorMode="Detailed"></httpErrors>
    <serverRuntime uploadReadAheadSize="1000485760" />
    <httpErrors errorMode="Detailed" />
    <asp scriptErrorSentToBrowser="true"/>
    <customErrors mode="Off"/>
    <compilation debug="true"/>

This is grand, as long as this specifier 29 actually works. You might need a different 28 solution, say, if you are hosting locally 27 with something other than IIS (like kestrel, or 26 even IIS express.)

Sometimes this configuration 25 section is locked as well, in which case 24 it can be unlocked using the following command 23 (run cmd as admin):

%windir%\system32\inetsrv\appcmd.exe unlock config -section:system.webServer/serverRuntime

This is also something 22 to consider when setting up prod. In some 21 cases, your configuration within web.config 20 within your code for the uploadReadAheadSize 19 can conflict with whatever is specified 18 in your prod instance. For me, I ended up 17 getting rid of my configuration within source 16 control, and instead, set the uploadReadAhead 15 value for prod during environment set up. I 14 did this by running the command (in cmd 13 as admin):

appcmd set config "https://example.com" /section:system.webserver/serverruntime /uploadreadaheadsize:500048576 /commit:apphost
appcmd set config "http://example.com" /section:system.webserver/serverruntime /uploadreadaheadsize:500048576 /commit:apphost

I also set uploadReadAheadSize 12 within C:\inetpub\wwwroot\web.config on 11 the prod server as well, so it looks something 10 like:

<?xml version="1.0" encoding="utf-8"?>
    <serverRuntime uploadReadAheadSize="500485760" />
    <!--If this line breaks, unlock the config by opening cmd as admin and running %windir%\system32\inetsrv\appcmd.exe unlock config -section:system.webServer/serverRuntime-->

This can also be set as per this screenshot:

[![enter 9 image description here][1]][1]

Now that you've 8 set this up, it's still entirely possible 7 that your application breaks, giving you 6 a 413 because even though IIS may be set up correctly, there are also upload limits within the .net core ecosystem which can throw an HTTP 414 error when handling large files. This is something I found 5 out [here][2]

The TLDR of this is that you 4 want to have the following code within your 3 startup.cs -> configureServices()

    services.Configure<IISServerOptions>(options => {
        options.MaxRequestBodySize = int.MaxValue;

    services.Configure<FormOptions>(x =>
        x.ValueLengthLimit = int.MaxValue;
        x.MultipartBodyLengthLimit = int.MaxValue;
        x.BufferBodyLengthLimit = int.MaxValue;
        x.MultipartBoundaryLengthLimit = int.MaxValue;

Note 2 that there is code in there for if you are 1 hosting for kestrel rather than IIS, also [1]: https://i.stack.imgur.com/yLHQQ.png [2]: https://github.com/dotnet/aspnetcore/issues/20369#issuecomment-607057822

Score: 0

This works for me. Set it in your web.config under 1 the configuration section:

            <requestLimits maxAllowedContentLength="999999999" />

More Related questions