Home kellton

Main navigation

  • Services
    • Digital Business Services
      • Digital Experience
        • Product Strategy & Consulting
        • Product Design
        • Product Management
      • Product Engineering
        • Digital Application Development
        • Mobile Engineering
        • IoT & Wearables Solutions
        • Quality Engineering
      • Data Engineering & AI
        • Data Engineering
        • Data Science & ML
        • Generative AI & ChatGPT
        • Visualisation & Analytics
        • Integration & API
        • RPA
      • Cloud Engineering
        • Cloud Consulting
        • Cloud Migration
        • Cloud Managed Services
        • DevSecOps
      • NextGen Services
        • Blockchain
        • Web3
        • Metaverse
    • SAP
      • SAP Services
        • S/4HANA Implementations
        • SAP AMS Support
        • SAP Automation
        • SAP Security & GRC
        • SAP Value Added Solutions
        • Other SAP Implementations
  • Platforms & Products
    • Kellton4Health
    • Kellton4NFT
    • Kellton4Commerce
    • KLGAME
    • tHRive
    • Optima
  • Industries
    • Fintech, Banking, Financial Services & Insurance
    • Retail, E-Commerce & Distribution
    • Pharma, Healthcare & Life Sciences
    • Non-Profit, Government & Education
    • Travel, Logistics & Hospitality
    • HiTech, SaaS, ISV & Communications
    • Manufacturing, Automotive & Chemicals
    • Oil,Gas & Mining
    • Energy & Utilities
  • Insights
    • Blogs
    • Brochures
    • Success Stories
    • News / Announcements
    • Webinars
    • White papers
  • Careers
    • Life At Kellton
    • Jobs
  • About
    • About Us
    • Our Partners
    • Our Leadership
    • Testimonials
    • Investors
    • Privacy-Policy
    • Contact Us
Search

Breadcrumb

  1. Home
  2. Blogs
  3. How to Submit a Large Number of Form-values in ...

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

Product Engineering
November 16th , 2017
Posted By:
Kellton
linkedin
How to Submit a Large Number of Form-values in ASP.NET Core?

Related Post

How blockchain-driven innovations can transform global supply chains
Blog
Breaking down barriers: How blockchain-driven innovations can transform global supply chains?
25 May, 2023
Generative AI and ChatGPT
Blog
Redefining the game: Generative AI and ChatGPT unlock new possibilities across industries
11 May, 2023
android testing frameworks
Blog
Best Android automated app testing frameworks in 2023
08 May, 2023

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.

Posted By:
Kellton
linkedin

Want to know more?

android testing frameworks
Blog
Best Android automated app testing frameworks in 2023
08 May, 2023
Blog image
Blog
10 Best Backend Web Development Frameworks for 2023
17 Apr, 2023
Blog image
Blog
What is Kubernetes? Discover the top 3 Kubernetes trends on the rise in 2023
20 Mar, 2023

Leading you through Digital Transformation journey

North America: +1.844.469.8900

Asia: +91.124.469.8900

Europe: +44.203.807.6911

Email: ask@kellton.com

Footer menu right

  • Digital Experience
  • Data Engineering & AI
  • Nextgen Services
  • About
  • Contact

Footer Menu Left

  • Product Engineering
  • Cloud Engineering
  • SAP Services
  • Careers
  • Success Stories

© 2023 Kellton