'submit large form data with post action return 400 error in .NET 6

I'm using .NET 6 to post a form with large form data (about 200Mb), and there is no any file.

this's kind of my form in front-end:

@using (Html.BeginForm(FormMethod.Post, new { id = "frm", @autocomplete = "off", @enctype="multipart/form-data" }))
{
    @Html.AntiForgeryToken()
...
}

and back-end:

[ValidateAntiForgeryToken]
[DisableRequestSizeLimit]
[HttpPost]
public async Task<IActionResult> SearchList(VM_SearchList data)

quoto from Skrface I have submited with an VerificationToken field in front-end, and decorated with a ValidateAntiForgeryToken filter in back-end, so it doesn't seems like a validation issus.

and from Matthew Steven Monkan and this answer, I have tried all the setting there, like DisableRequestSizeLimit filter above, and also tried to decorate with RequestFormLimits and RequestSizeLimit filters which set to 500Mb.

[ValidateAntiForgeryToken]
[RequestFormLimits(MultipartBodyLengthLimit = 524288000)]
[RequestSizeLimit(524288000)]
[HttpPost]
public async Task<IActionResult> SearchList(VM_SearchList data)

And also tried to set in Program.cs:

builder.Services.Configure<HttpSysOptions>(options =>
{
    options.MaxRequestBodySize = int.MaxValue;

});

builder.Services.AddMvc();
builder.Services.Configure<FormOptions>(x =>
{
    x.ValueLengthLimit = int.MaxValue;
    x.MultipartBodyLengthLimit = int.MaxValue;
    x.MemoryBufferThreshold = int.MaxValue;
});

or in KestrelServer:

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

but it still can't work...

here is my request body information and error:

enter image description here error_picture

I can post the form successfully with the form data that request content length is about 80Mb, but size like above (143,118) can't work, do I miss something else?

can anyone help? thanks a lot!

I've tried, the maximum form data size that I can post is about 130Mb (content-length: 133120)

Edited: Here is my web.config on IIS server.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="dotnet" arguments=".\BASE.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
      <security>
        <requestFiltering>
          <!-- This will handle requests up to 500MB -->
          <requestLimits maxAllowedContentLength="524288000" />
        </requestFiltering>
      </security>
    </system.webServer>
  </location>
</configuration>
<!--ProjectGuid: bfaa51b3-9cb0-4908-8d5e-6289bd4f329a-->


Solution 1:[1]

Finally, Steven solves my problem. Thanks a lot!

It's quite a difference to change limitation of form data size (not file size).

You can use only RequestFormLimitsAttribute.ValueCountLimit to decorate the controller, or set FormOptions.ValueCountLimit in Program.cs in .NET 6 or Startup.cs in .NET 5 for your full site.

I have tried either way above, it will work!

Here's the sample code

  • Controller:
[ValidateAntiForgeryToken]
[RequestFormLimits(ValueCountLimit = int.MaxValue)]
[HttpPost]
public async Task<IActionResult> SearchList(VM_SearchList data)

  • Program.cs (.NET 6):
builder.Services.Configure<FormOptions>(x =>
{
    x.ValueCountLimit = int.MaxValue;
});

  • startup.cs (.NET 5):
services.Configure<FormOptions>(options => 
{
??options.ValueCountLimit = int.MaxValue
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1