Search code examples
azurefile-uploadazure-web-app-service.net-6.0http-status-code-413

dotnet6 on Azure WebApp - http error 413 request entity too large


I'm getting the following error while uploading large (38 MB) file using file upload on .NET 6 application deployed on Azure using Chrome browser:

enter image description here

I googled and found 2 solutions. It works fine in my local environment but not working after I deployed it to Azure.

Solution 1: In Program.cs file

builder.Services.Configure<KestrelServerOptions>(options =>
{
    options.Limits.MaxRequestBodySize = long.MaxValue;
});
builder.Services.Configure<IISServerOptions>(options =>
{
    options.MaxRequestBodySize = long.MaxValue;
});

Solution 2: ConfigureServices method in Startup.cs file

services.Configure<FormOptions>(options =>
            {
                options.MultipartBodyLengthLimit = long.MaxValue;
            });

Solution

  • To work with large files, we need to make sure the maxAllowedContentLength and maxRequestLength is set correctly according to the request and file size in Web.config file.

    maxRequestLength in <system.web>:

    <system.web>
        <httpRuntime maxRequestLength="1073741824"/>
    </system.web>
    

    Thanks @Thomas for the comments

    maxAllowedContentLength in security section

    As mentioned in the MSDoc, Add maxAllowedContentLength in the security tag.

    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="2147483648" />
      </requestFiltering>
    </security>  
    

    Thanks @[bhawanisingh] for clear steps.

    Refer this blog for more details on how to work with large files.

    I have .Net6 application and It doesn't have web.config file in the solution

    When you deploy .Net Core App to Azure App Service (Windows / Linux),web.config file be generated automatically in the deployed files (with basic settings).

    Windows: enter image description here

    Linux: enter image description here

    You can even add the web.config file locally in the application root directory and deploy along with other files.