Max Request Form Size Issue in ASP.NET Core MVC 3

Viewed 2026

I just ported a web application from ASP.NET Core 2.1 to 3.0 and I'm getting that form key length error:

InvalidDataException: Form key length limit 2048 or value length limit 2147483647 exceeded. Microsoft.AspNetCore.WebUtilities.FormPipeReader.ThrowKeyOrValueTooLargeException()

In my startup.cs I've got the following snippet:

services.AddMvc()
    .SetCompatibilityVersion( CompatibilityVersion.Version_3_0 )
    .AddSessionStateTempDataProvider()
      // Maintain property names during serialization. See:
      // https://github.com/aspnet/Announcements/issues/194
      .AddNewtonsoftJson( options =>
      {
        options.SerializerSettings.ContractResolver = new DefaultContractResolver();
        options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
      } );


  // maxout form sizes
  services.Configure<FormOptions>( options =>
  {
    options.ValueCountLimit = int.MaxValue;
    options.ValueLengthLimit = int.MaxValue;
    options.MultipartHeadersLengthLimit = int.MaxValue;
  } );

But, as in the past, this doesn't seem to be working again. Maybe I've got the wrong order for where the form options need to be placed?

I'd rather not decorate the actions of 100 controls, if possible; I'd rather set this globally.

1 Answers

Try adding an additional option for KeyLengthLimit, that fixed it for me.

services.Configure<FormOptions>(options =>
{
    options.KeyLengthLimit = int.MaxValue;
    options.ValueCountLimit = int.MaxValue;
    options.ValueLengthLimit = int.MaxValue;
    options.MultipartHeadersLengthLimit = int.MaxValue;
});
Related