Form submit resulting in “InvalidDataException: Form value count limit 1024 exceeded.”

前端 未结 4 1485
广开言路
广开言路 2020-12-05 17:01

I have created an mvc site and I\'m posting a large amount of json form data (Content-Type:application/x-www-form-urlencoded) back to the mvc controller. When I

相关标签:
4条回答
  • 2020-12-05 17:48

    Update: The MVC SDK now includes this functionality via RequestSizeLimitAttribute. There is no longer any need to create a custom attribute.

    Thanks to andrey-bobrov for pointing this out in a comment. The original answer is below, for posterity.


    You can change the default formvalue limit using the FormOptions. If you are using MVC, then you can create a filter and decorate on action where you want to extend this limit and keep the default for rest of the actions.

    /// <summary>
    /// Filter to set size limits for request form data
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
    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)
            {
                // Request form has not been read yet, so set the limits
                features.Set<IFormFeature>(new FormFeature(context.HttpContext.Request, _formOptions));
            }
        }
    }
    

    Action:

    [HttpPost]
    [RequestFormSizeLimit(valueCountLimit: 2000)]
    public IActionResult ActionSpecificLimits(YourModel model)
    

    NOTE: If your action needs to support Antiforgery validation too, then you would need to order the filters. Example:

    // Set the request form size limits *before* the antiforgery token validation filter is executed so that the
    // limits are honored when the antiforgery validation filter tries to read the form. These form size limits
    // only apply to this action.
    [HttpPost]
    [RequestFormSizeLimit(valueCountLimit: 2000, Order = 1)]
    [ValidateAntiForgeryToken(Order = 2)]
    public IActionResult ActionSpecificLimits(YourModel model)
    
    0 讨论(0)
  • 2020-12-05 17:51

    The default formvalue(not formkey) limit is 1024.

    Also, I think you can just change the FormOptions limit in Startup.cs file.

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<FormOptions>(options =>
        {
            options.ValueCountLimit = int.MaxValue;
        });
    }
    
    0 讨论(0)
  • 2020-12-05 17:56

    In my case, it worked by changing ValueLengthLimit, in Startup.cs file

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<FormOptions>(options =>
        {
            options.ValueCountLimit = 200; // 200 items max
            options.ValueLengthLimit = 1024 * 1024 * 100; // 100MB max len form data
        });
    
    0 讨论(0)
  • 2020-12-05 17:59

    If you are using .net core 2.1 or above, you can use the built in RequestFormLimits attribute as shown below on a controller or action-

    [RequestFormLimits(ValueCountLimit = 5000)]
    public class TestController: Controller
    

    Link to official docs

    0 讨论(0)
提交回复
热议问题