表单提交导致“ InvalidDataException:表单值计数限制超过1024”。 [英] Form submit resulting in "InvalidDataException: Form value count limit 1024 exceeded."

查看:628
本文介绍了表单提交导致“ InvalidDataException:表单值计数限制超过1024”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个mvc网站,并发布了大量的json表单数据( Content-Type:application / x-www-form-urlencoded ) b $ b回到mvc控制器。当我这样做时,我收到500响应,指出: InvalidDataException:超出表单值计数限制1024。

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 do this, I receive a 500 response that states: "InvalidDataException: Form value count limit 1024 exceeded."

在以前版本的aspnet中,应添加以下内容到web.config中以增加限制:

In previous versions of aspnet, you would add the following to the web.config to increase the limit:

<appSettings>
    <add key="aspnet:MaxHttpCollectionKeys" value="5000" />
    <add key="aspnet:MaxJsonDeserializerMembers" value="5000" />
</appSettings>

当我将这些值放在w​​eb.config中时,看不到任何变化,所以我我猜微软将不再从web.config中读取这些值。
但是,我不知道应在何处设置这些设置。

When I put these values in the web.config, I do not see any change, so I'm guessing Microsoft is no longer reading these values out of the web.config. However, I cannot figure out where these settings should be set.

对于增加表单值计数的任何帮助,我们将不胜感激!

Any help with increasing the form value count is greatly appreciated!

要清楚,当我的帖子数据中的项目数少于1024时,此请求可以很好地工作。

To be clear, this request works perfectly fine when the number of items in my post data is less than 1024.

推荐答案

更新::MVC SDK现在通过 RequestSizeLimitAttribute 包含了此功能。不再需要创建自定义属性。

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

感谢 andrey-bobrov 评论。出于后代考虑,原始答案在下面。

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

您可以使用更改默认表单价值限制FormOptions 。如果您使用的是MVC,则可以创建过滤器并在要扩展此限制的操作上进行修饰,并保留其余操作的默认值。

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

操作

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

这篇关于表单提交导致“ InvalidDataException:表单值计数限制超过1024”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆