绑定到“模型”的集合超过MvcOptions.MaxModelBindingCollectionSize1024

问题描述

向控制器提交包含1024个以上的项的数组(当前为2500个)时,我遇到了异常。似乎您可以提交的项目数上限为1024。

它似乎是在MvcOptions中设置的,但是我使用的是.Net Core 3.0和端点路由,所以我无法通过UseMVC访问MvcOptions。

如何提高此限制?

我之前通过添加helper属性提高了限制,如下所示。但是我不确定在哪里需要设置此特定限制-它似乎不是HttpContext.Features的一部分。

    public void OnAuthorization(AuthorizationFilterContext context)
    {
        var features = context.HttpContext.Features;
        var formFeature = features.Get<IFormFeature>();
        if (formFeature == null || formFeature.Form == null)
        {
            features.Set<IFormFeature>(new FormFeature(context.HttpContext.Request,_formOptions));
        }
    }

解决方法

对于.net core 3.1

在启动文件中更新以下代码

  services.AddMvc(options =>
            {
                
                options.MaxModelBindingCollectionSize = int.MaxValue;

            });
,

在Startup#ConfigureServices

services.Configure<FormOptions>(options => options.ValueCountLimit = 5000); // select your max limit

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...