3

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));
        }
    }
Gavin Coates
  • 1,366
  • 1
  • 20
  • 44

2 Answers2

10

For .net core 3.1

Update below code in startup file

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

            });
Nilesh Gajare
  • 6,302
  • 3
  • 42
  • 73
  • This snippet caused the affected action to stall on post back never reaching the start of the action. – John Sep 27 '22 at 11:40
1

In Startup#ConfigureServices

services.Configure<FormOptions>(options => options.ValueCountLimit = 5000); // select your max limit
Roar S.
  • 8,103
  • 1
  • 15
  • 37
  • 1
    Thanks.. that works. Out of interest how would you do it as an attribute so it can be applied to a specific function, rather than site wide? – Gavin Coates Aug 12 '20 at 10:00
  • 1
    Hi Gavin, nice to hear that it solved your problem. Regarding your attribute-question; I seldom write custom atttributes because I like to keep code as standard as possible. Hence, my attribute-skills are not at a level where I should give advices :-) – Roar S. Aug 12 '20 at 11:48