aspnet / Mvc

[Archived] ASP.NET Core MVC is a model view controller framework for building dynamic web sites with clean separation of concerns, including the merged MVC, Web API, and Web Pages w/ Razor. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
5.62k stars 2.14k forks source link

413 Payload Too Large on ASP.NET 2.0 #6623

Closed maroallegro closed 7 years ago

maroallegro commented 7 years ago

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

rynowak commented 7 years ago

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.

Eilon commented 7 years ago

This issue was moved to aspnet/KestrelHttpServer#1982