Form submit resulting in “InvalidDataException: Form value count limit 1024 exceeded.” In ASP.NET Core
You can change the default formvalue limit using the
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)
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.
namespace Filter
{
using System;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc.Filters;
/// <summary>
/// Request Form Size Limit Attribute
/// </summary>
/// <seealso cref="System.Attribute" />
/// <seealso cref="Microsoft.AspNetCore.Mvc.Filters.IAuthorizationFilter" />
/// <seealso cref="Microsoft.AspNetCore.Mvc.Filters.IOrderedFilter" />
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class RequestFormSizeLimitAttribute : Attribute, IAuthorizationFilter
{
/// <summary>
/// The form options
/// </summary>
private readonly FormOptions formOptions;
/// <summary>
/// Initializes a new instance of the <see cref="RequestFormSizeLimitAttribute"/> class.
/// </summary>
/// <param name="valueCountLimit">The value count limit.</param>
public RequestFormSizeLimitAttribute(int valueCountLimit)
{
this.formOptions = new FormOptions()
{
ValueCountLimit = valueCountLimit
};
}
/// <summary>
/// Called early in the filter pipeline to confirm request is authorized.
/// </summary>
/// <param name="context">The <see cref="T:Microsoft.AspNetCore.Mvc.Filters.AuthorizationFilterContext" />.</param>
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, this.formOptions));
}
}
}
}
{
using System;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc.Filters;
/// <summary>
/// Request Form Size Limit Attribute
/// </summary>
/// <seealso cref="System.Attribute" />
/// <seealso cref="Microsoft.AspNetCore.Mvc.Filters.IAuthorizationFilter" />
/// <seealso cref="Microsoft.AspNetCore.Mvc.Filters.IOrderedFilter" />
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class RequestFormSizeLimitAttribute : Attribute, IAuthorizationFilter
{
/// <summary>
/// The form options
/// </summary>
private readonly FormOptions formOptions;
/// <summary>
/// Initializes a new instance of the <see cref="RequestFormSizeLimitAttribute"/> class.
/// </summary>
/// <param name="valueCountLimit">The value count limit.</param>
public RequestFormSizeLimitAttribute(int valueCountLimit)
{
this.formOptions = new FormOptions()
{
ValueCountLimit = valueCountLimit
};
}
/// <summary>
/// Called early in the filter pipeline to confirm request is authorized.
/// </summary>
/// <param name="context">The <see cref="T:Microsoft.AspNetCore.Mvc.Filters.AuthorizationFilterContext" />.</param>
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, this.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)
Comments
Post a Comment