43

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

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

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

当我将这些值放入 web.config 时,我看不到任何变化,所以我猜测 Microsoft 不再从 web.config 中读取这些值。但是,我不知道应该在哪里设置这些设置。

非常感谢任何有关增加表单值计数的帮助!

需要明确的是,当我的帖子数据中的项目数少于 1024 时,此请求可以正常工作。

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

4

6 回答 6

66

默认的formvalue(不是 formkey)限制为 1024。

另外,我认为您可以更改Startup.cs文件FormOptions中的限制。

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<FormOptions>(options =>
    {
        options.ValueCountLimit = int.MaxValue;
    });
}
于 2018-04-28T14:54:24.610 回答
25

更新: MVC SDK 现在通过RequestSizeLimitAttribute. 不再需要创建自定义属性。

感谢andrey-bobrov在评论中指出这一点。原始答案如下,供后人参考。


您可以使用FormOptions. 如果您使用的是 MVC,那么您可以创建一个过滤器并在您想要扩展此限制的操作上进行装饰,并为其余操作保留默认值。

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

注意:如果您的操作也需要支持防伪验证,那么您需要订购过滤器。例子:

// 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)
于 2016-07-13T19:24:08.910 回答
23

如果您使用的是 .net core 2.1 或更高版本,则可以在控制器或操作上使用内置的 RequestFormLimits 属性,如下所示 -

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

链接到官方文档

于 2019-04-02T15:27:36.177 回答
21

就我而言,它通过更改 Startup.cs 文件中的 ValueLengthLimit 来工作

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
    });
于 2018-09-28T21:07:00.190 回答
4

使用 .net core 3.1,您还需要

services.Configure<FormOptions>(options =>
{
    options.ValueCountLimit = int.MaxValue;
});

services.AddMvc(options =>
{
    options.MaxModelBindingCollectionSize = int.MaxValue;
});

在这里找到:https ://stackoverflow.com/a/64500089/14958019

只有使用MaxModelBindingCollectionSize我才能得到我的 json 对象,其中超过 1024 行完全从带有 ajax 的 javascript 传递到 mvc 控制器。

于 2021-01-07T10:46:56.470 回答
1

表单值计数限制基本上是总数。您在请求中传递的参数。

可以从 Startup.cs 设置限制:

 services.Configure<FormOptions>(options =>
            {
                options.ValueCountLimit = 199;
            });

见下图,我在一个请求中传递了 200 个参数。默认限制为 1024,但我已将其设置为 199,因此我传递了超过 199 个参数,然后它会出错。

在此处输入图像描述

在此处输入图像描述

于 2021-04-21T11:59:16.277 回答