今天在.NET Core Web应用中提交大表单时,抛出了如下的错误信息
System.IO.InvalidDataException: Form value count limit 1024 exceeded. at Microsoft.AspNetCore.WebUtilities.FormReader.Append(KeyValueAccumulator& accumulator) at Microsoft.AspNetCore.WebUtilities.FormReader.ReadFormAsync(CancellationToken cancellationToken) at Microsoft.AspNetCore.Http.Features.FormFeature.InnerReadFormAsync(CancellationToken cancellationToken) at Microsoft.AspNetCore.Mvc.ModelBinding.FormValueProviderFactory.AddValueProviderAsync(ValueProviderFactoryContext context) at Microsoft.AspNetCore.Mvc.ModelBinding.CompositeValueProvider.CreateAsync(ActionContext actionContext, IList`1 factories) at Microsoft.AspNetCore.Mvc.ModelBinding.CompositeValueProvider.CreateAsync(ControllerContext controllerContext) at Microsoft.AspNetCore.Mvc.Internal.ControllerBinderDelegateProvider.<>c__DisplayClass0_0.<g__Bind|0>d.MoveNext()--- End of stack trace from previous location where exception was thrown --- at Microsoft.AspNetCore.Mvc.Internal
出现这个错误的原因在于.NET Core对提交的表单项数量进行了限制,最大数是1024,有两种方法解决。
Action级别
在Action上使用 使用RequestFormSizeLimit 设置
[HttpPost]
[RequestFormSizeLimit(valueCountLimit: 2000)]
public IActionResult ActionSpecificLimits(YourModel model)
应用程序级别
public void ConfigureServices(IServiceCollection services)
{
services.Configure<FormOptions>(options =>
{
options.ValueCountLimit = 5000; // 5000 items max
options.ValueLengthLimit = 1024 * 1024 * 100; // 100MB max len form data
});