简体   繁体   中英

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?

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>

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.

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