Search code examples
azurefile-uploadasp.net-core-3.1http-status-code-413

Change file upload limits for an ASP.NET Core 3.1 App using Azure App Service


I have a solution built using ASP.NET Core 3.1. The pipeline is built to publish to an App Service in Azure.

The trouble is, we are unable to upload a file size larger than 28MB. We get a 413 error - file too large. This seems to be an IIS issue, but Azure App Services don't use IIS.

The specific error returned in the browser is this:

Request URL: https:xxxxxxxx
Request Method: POST
Status Code: 413 Request Entity Too Large
Remote Address: xx.xx.x.x
Referrer Policy: strict-origin-when-cross-origin
Content-Length: 67
Content-Type: text/html
Date: Sat, 23 Apr 2022 16:15:06 GMT
Server: Microsoft-IIS/10.0
Set-Cookie: 
Set-Cookie: 
X-Powered-By: ASP.NET
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Connection: keep-alive
Content-Length: 31438702

Our steps to remediate the problem include adding a web.config file to our wwwroot directory, as follows:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="52428800" />
      </requestFiltering>
    </security>
  </system.webServer>
</configuration>

We then added the following code to the Startup file in ConfigureServices:

   services.Configure<IISServerOptions>(options =>
            {
                options.MaxRequestBodySize = 104857600;
            });

            services.Configure<FormOptions>(options =>
            {
                // Set the limit to 100 MB
                options.MultipartBodyLengthLimit = 104857600;
                options.ValueLengthLimit = 104857600;
                options.MultipartHeadersLengthLimit = 104857600;
            });

Neither of these has changed the result. We also found a site that suggested adding the code below to Configure() in the Startup. but this code broke other routines, so we took it out.

   //app.Use(async (context, next) =>
            //{
            //    context.Features.Get<IHttpMaxRequestBodySizeFeature>().MaxRequestBodySize = 104857600;
            //    await next.Invoke();
            //});

Our last step was to request support from Microsoft. Their response confirmed that Azure App Services do not use IIS, and this code should work. They also reviewed our Azure configuration and reported there are no changes that need to be made there.

So, what have I done wrong or left out, or missed?


Solution

  • This how we set it up in NET6, should work very similar in 3.1:

    progam.cs

    builder.WebHost.ConfigureKestrel(options =>
    {
       options.Limits.MaxRequestBodySize = 256_000_000;
    })
    .UseIISIntegration()
    

    web.config

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <system.webServer>
        <security>
          <requestFiltering>
            <requestLimits maxAllowedContentLength="256000000" />
          </requestFiltering>
        </security>
      </system.webServer>
    </configuration>