Collection Bound to 'Model' Exceeded MvcOptions.MaxModelBindingCollectionSize (1024)

Viewed 2364

I'm getting an exception when submitting an array of more than 1024 items to a controller (currently 2,500 items). It seems there is a max limit on the number of items you can submit of 1024.

It seems to be set in MvcOptions, however I'm using .Net Core 3.0 and using endpoint routing, so I don't have access to MvcOptions through UseMVC.

How can I raise this limit?

I've raised limits before by adding a helper attribute, as follows. However I'm not sure where I would need to set this particular limit - it doesnt appear to be part of 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));
        }
    }
2 Answers

For .net core 3.1

Update below code in startup file

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

            });

In Startup#ConfigureServices

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