表单提交导致“InvalidDataException:超出表单值计数限制1024”。

问题描述 投票:16回答:4

我创建了一个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>

当我将这些值放在w​​eb.config中时,我没有看到任何更改,所以我猜测Microsoft不再从web.config中读取这些值。但是,我无法弄清楚应该在哪里设置这些设置。

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

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

.net appsettings asp.net-core-1.0
4个回答
18
投票

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

感谢andrey-bobrov将其指向comment。对于子孙后代,原始答案如下。


您可以使用FormOptions更改默认的formvalue限制。如果您正在使用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)

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

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

31
投票

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

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

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

8
投票

在我的例子中,它通过在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
    });

5
投票

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

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

链接到官方文档 - https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.requestformlimitsattribute?view=aspnetcore-2.1

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.