Developer Webs
Developer Webs

Reputation: 1021

C# ASP.NET Core MVC website sometimes receives NULL instead of POSTED value

I've a C# ASP.NET Core MVC website. I'm running it in Visual Studio. I'm posting to a controller action that takes parameter string[] identifiers. The HTML form element is a textarea that has values like:

one
two
three

This works fine, and identifiers is properly populated with 3 entries.

If the textarea instead has 37,000+ lines (1.4 MB when saved to disk as a text file), then the controller sees identifiers as NULL.

In my Startup.cs I've used (and have also used with just the defaults):

services.Configure<IISServerOptions>(o =>
{
    o.MaxRequestBodySize = int.MaxValue;
});

services.Configure<KestrelServerOptions>(o =>
{
    o.Limits.MaxRequestBodySize = int.MaxValue;
});

services.Configure<FormOptions>(o =>
{
    o.ValueLengthLimit = int.MaxValue;
    o.MultipartBodyLengthLimit = int.MaxValue;
});

And my web.config (I'm running this from Visual Studio, and web.config is set to "Copy Always") contains:

<configuration>
  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="1073741824" />
      </requestFiltering>
    </security>
</configuration>

There are no errors or warnings in the Windows Event Viewer, so I suspect it's an issue in code?

Assuming the issue is related to max POST size, where have I gone wrong?

Update The data is posted with jQuery ajax, with no contentType specified. I think the default is: 'application/x-www-form-urlencoded; charset=UTF-8' which likely limits the request to 2k. Though explicitly setting a contentType doesn't seem to help.

Upvotes: 0

Views: 138

Answers (1)

Developer Webs
Developer Webs

Reputation: 1021

To fix the issue I had to add the following to my controller:

[RequestFormLimits(ValueCountLimit = int.MaxValue)]

I had thought the code below would do the same thing, but it does not solve the problem. Note that services is an instance of IServiceCollection and is used in my Startup.cs (where we also inject our settings, database connection, set session details, etc).

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

Upvotes: 0

Related Questions