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

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

问题描述

我创建了一个 mvc 站点并且我发布了大量的 json 表单数据 (Content-Type:application/x-www-form-urlencoded)回到 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>

当我将这些值放入 web.config 时,我没有看到任何变化,所以我猜测 Microsoft 不再从 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.

更新:在 asp.net MVC Core 3.1 中,错误消息是 -无法读取请求表单.超出表单值计数限制 1024."

Update: In asp.net MVC Core 3.1 the error message is - "Failed to read the request form. Form value count limit 1024 exceeded."

推荐答案

更新: 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天全站免登陆