2

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?

jimd12
  • 25
  • 1
  • 6
  • Instead of setting the specific numeric value, change to `int.MaxValue` for the object variables of `services.Configure – Delliganesh Sevanesan Apr 25 '22 at 05:52
  • Thank you for your reply. I read in online post that setting the int.MaxValue opens me up to greater risk of cybersecurity problems. Is that incorrect? – jimd12 Apr 25 '22 at 13:14
  • Refer [here](https://stackoverflow.com/a/60964837/15997690) as using `int.MaxValue` workaround helped a lot of people to overcome this issue. – Delliganesh Sevanesan Apr 25 '22 at 14:27
  • Thank you for your persistence. I had used this post you sited before, but I went back and reviewed it again. I found a discrepancy in the placement of the webconfig file between this post and the Microsoft documentation for the same issue. The Microsoft solution said to place the webconfig in the wwwroot directory. the example you showed me had placed in the Project directory. I moved the file and the upload works! – jimd12 Apr 26 '22 at 20:19
  • Just want to point out that, yes, Azure App services uses IIS. As of now, it is running IIS 10. You can confirm this by looking at the response header under the "Server" `Microsoft-IIS/10.0` – Greg Gum Apr 02 '23 at 00:29

2 Answers2

2

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>

Alex AIT
  • 17,361
  • 3
  • 36
  • 73
0

Glad @jimd12, after our discussion we came to know that your issue got resolved.

You found the discrepancy in the placement of the web.config file and placed it in the wwwroot directory, which you misplaced in the Project directory, and then the required file size upload worked.

Posting as an answer so it will help if anyone facing this similar issue. even if you have allowed the content length to high value, please check the placement of web.config file and refer to this SO thread that contains a few workarounds of overcoming the issue of file upload limits for Azure App Service.

Delliganesh Sevanesan
  • 4,146
  • 1
  • 5
  • 15
  • I wasn't clear enough when I stated where I had to move the webconfig file. I had to move it to the project directory as your reference described, and take it out of the wwwroot directory as the Microsoft reference described. The rest of your understanding of my resolution was correct. – jimd12 Apr 27 '22 at 22:23