How to Submit a Large Number of Form-values in ASP.NET Core?

Nov 16, 2017 posted by Sonu Malik under Microsoft Technologies

HTML forms use either GET or POST method to submit form values to the server. The default method that most of the developers use is GET. When using the GET method, the form data is encoded in the URI as a query string. However, when using the POST method, the form data is placed in the request body.

If you wish to submit a form with a large number of elements (say 500 checkboxes) to our controller post method, then we get a runtime exception: InvalidDataException: Form value count limit 1024 exceeded. This exception means that ASP.NET Core does not allow us to post data which have value and key length above 1024. In order for the developer to explicitly enable this, we can use the following two ways:

  1. Application Level Changes:  Add the code in the startup.cs file as it will apply by default to all controller methods in the application.

 

public void ConfigureServices(IServiceCollection services)

{

    services.Configure<FormOptions>(x => x.ValueCountLimit = 10000);

    services.AddMvc();

}

 

  1. Method Level Changes: To increase the form size limit at the method level, we need to create a custom attribute say “RequestFormSizeLimitAttribute” in the controller post method.

  public class RequestFormSizeLimitAttribute : Attribute, IAuthorizationFilter,    

       IOrderedFilter

    {

        private readonly FormOptions _formOptions;

 

        public RequestFormSizeLimitAttribute(int valueCountLimit)

        {

            _formOptions = new FormOptions()

            {

                ValueCountLimit = valueCountLimit

            };

        }

 

        public int Order { get; set; }

 

        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));

            }

        }

   }

 

Once the attribute is ready, we need to increase the key length which can be done in the following two ways:

 

  1. If no “Antiforgery” token is applied to the controller method, then the attribute can be applied directly on the method without specifying “order” of execution.

 

[HttpPost]

[RequestFormSizeLimit(valueCountLimit: 20000)]

public IActionResult ActionSpecificLimits(YourModel model)

 

  1. If “Antiforgery” token is applied to the controller method, then we need to specify the order in which the attributes will be executed. If the order is not mentioned then the following exception is raised: “InvalidDataException: Form value count limit 1024 exceeded.”

It is mandatory to execute the “RequestFormSizeLimitAttribute” attribute before the “Antiforgery” token validation filter so that the limits are honored when the antiforgery validation filter tries to read the form. The form size limits only apply to this action.

[HttpPost]

[RequestFormSizeLimit(valueCountLimit: 20000, Order = 1)]

[ValidateAntiForgeryToken(Order = 2)]

public IActionResult ActionSpecificLimits(YourModel model)

LearnMore.jpg

Digital Transformation comprises the largest portfolio of work for Kellton Tech. Learn more about how our digital expertise is increasing ROI of clients looking for .NET development services.

 

Share this: