I recently converted a .Net http handler to a .Net Core Middleware Component and am having trouble when uploading files. I'm getting the following error on this statement:
if (context.Request.Form.ContainsKey(param))
System.IO.InvalidDataException: Form key length limit 2048 or value length limit 4194304 exceeded.
It appears that the entries in web.config of the .Net handler don't work in Core:
<httpRuntime requestValidationMode="2.0" maxRequestLength="2097151" executionTimeout="9999999" requestLengthDiskThreshold="10024000" />
<requestFiltering>
<requestLimits maxAllowedContentLength="1024000000" maxQueryString="32768" maxUrl="65536" />
</requestFiltering>
I've seen where there may be issues with that in 3.1 and higher. Other references about creating a custom attribute to explicitly set key/value length limit. And others that simply used DisableRequestSizeLimit. Unfortunately, their examples referenced MVC applications and mine is very different. Here is the start of the code:
private readonly RequestDelegate _next;
public FileManager(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
....
I tried putting [DisableRequestSizeLimit] immediately before the public async Task Invoke but that still gave the error.
Can someone let me know what and where I need to add the higher limits to upload large files?.. or what other information I can provide?
EDIT FOR ANSWER:
I awarded the correct answer to Ruikai Feng but to make it clear for any future readers, here's the details.
Code was inside startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<FormOptions>(x => { x.BufferBodyLengthLimit = long.MaxValue; x.ValueLengthLimit = int.MaxValue; x.KeyLengthLimit = int.MaxValue; x.MultipartBodyLengthLimit = long.MaxValue; })
I got carried away with maxing out all the values just to get it to work but it appeared x.ValueLengthLimit and x.KeyLengthLimit were the key players.
CodePudding user response:
You could try to set as below:
services.Configure<FormOptions>(x => { x.KeyLengthLimit = newkeylength;x.MultipartBodyLengthLimit = newcontentlength; });
You could check this document related with file upload for more details