aspnet / KestrelHttpServer

[Archived] A cross platform web server for ASP.NET Core. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
2.62k stars 528 forks source link

413 Payload Too Large on ASP.NET 2.0 #1982

Closed Eilon closed 7 years ago

Eilon commented 7 years ago

From @maroallegro on August 3, 2017 12:49

WebApi created in .NetCore 2.0 by default references AspNetCore 2.0:

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0-preview2-final" />
  </ItemGroup>

When we will not use IIS, but make startup project which starts WebApi:

    class Program
    {
        static void Main(string[] args)
        {
            BuildWebHost().Run();
        }

        private static IWebHost BuildWebHost()
        {
            return new WebHostBuilder().UseContentRoot(Directory.GetCurrentDirectory()).UseKestrel()
                .UseStartup<Startup>().UseUrls("http://localhost:59039/").Build();
        }
    }

it is impossible to send 60 MB file, because WebApi throws 413 Payload Too Large error.

Situation occurs despite of: -changing maxAllowedContentLength in web.config to bigger value -changing FormOptions in Startup.cs file

        public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.Configure<FormOptions>(
                options =>
                    {
                        options.MultipartBodyLengthLimit = 80000000;
                        options.ValueLengthLimit = int.MaxValue;
                        options.MultipartHeadersLengthLimit = int.MaxValue;
                    });
            services.AddMvc();
        }

-moving web.config files to root folders

Problem do not occur in .NETCore 1.1 WebApi because they references to AspNetCore 1.1:

  <ItemGroup>
    <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.2" />
  </ItemGroup>

To solve the problem in .NetCore 2.0 WebApi we can change:

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0-preview2-final" />
  </ItemGroup>

to:

  <ItemGroup>
    <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.2" />
  </ItemGroup>

Please see projects below which shows the problem. When running specific WebApi set Starter project as Startup project.

FileTransferTestApp.zip

Copied from original issue: aspnet/Mvc#6623

Eilon commented 7 years ago

From @rynowak on August 4, 2017 14:45

In 2.0.0 Kestrel enforces a maximum request size for security. This is in addition to the limits on form-data which we had in 1.1.0.

You can configure this globally - https://github.com/aspnet/KestrelHttpServer/blob/dev/src/Kestrel.Core/KestrelServerLimits.cs#L152 Or on a specific action - https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNetCore.Mvc.Core/RequestSizeLimitAttribute.cs

/cc @halter73 @Tratcher - does this need an announcement? I looked for one and could not find it.

Tratcher commented 7 years ago

https://docs.microsoft.com/en-us/aspnet/core/aspnetcore-2.0#maximum-request-body-size

halter73 commented 7 years ago

I created an announcement at aspnet/Announcements#267.