Mic*_*her 28 .net appsettings asp.net-core-1.0
我创建了一个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>
Run Code Online (Sandbox Code Playgroud)
当我将这些值放在web.config中时,我没有看到任何更改,所以我猜测Microsoft不再从web.config中读取这些值.但是,我无法弄清楚应该在哪里设置这些设置.
任何有关增加表单值计数的帮助都非常感谢!
需要说明的是,当我的帖子数据中的项目数小于1024时,此请求可以正常工作.
小智 37
默认的formvalue(非formkey)限制为1024.
此外,我认为您可以更改Startup.cs文件中的FormOptions
限制.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<FormOptions>(options =>
{
options.ValueCountLimit = int.MaxValue;
});
}
Run Code Online (Sandbox Code Playgroud)
Kir*_*lla 22
更新: MVC SDK现在包含此功能RequestSizeLimitAttribute
.不再需要创建自定义属性.
感谢andrey-bobrov在评论中指出这一点.对于子孙后代,原始答案如下.
您可以使用更改默认的formvalue限制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));
}
}
}
Run Code Online (Sandbox Code Playgroud)
行动:
[HttpPost]
[RequestFormSizeLimit(valueCountLimit: 2000)]
public IActionResult ActionSpecificLimits(YourModel model)
Run Code Online (Sandbox Code Playgroud)
注意:如果您的操作也需要支持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)
Run Code Online (Sandbox Code Playgroud)
Wag*_*ira 13
就我而言,它通过更改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
});
Run Code Online (Sandbox Code Playgroud)
小智 9
对于 .net core 3.1,您还需要
services.Configure<FormOptions>(options =>
{
options.ValueCountLimit = int.MaxValue;
});
Run Code Online (Sandbox Code Playgroud)
还
services.AddMvc(options =>
{
options.MaxModelBindingCollectionSize = int.MaxValue;
});
Run Code Online (Sandbox Code Playgroud)
在这里找到:https ://stackoverflow.com/a/64500089/14958019
仅使用MaxModelBindingCollectionSize,我才能获得包含超过 1024 行的 json 对象,该对象完全从带有 ajax 的 javascript 传递到 mvc 控制器。
小智 8
如果您使用的是.net core 2.1或更高版本,则可以在控制器或操作上使用内置的RequestFormLimits属性,如下所示:
[RequestFormLimits(ValueCountLimit = 5000)]
public class TestController: Controller
Run Code Online (Sandbox Code Playgroud)
链接到官方文档-https: //docs.microsoft.com/zh-cn/dotnet/api/microsoft.aspnetcore.mvc.requestformlimitsattribute? view = aspnetcore- 2.1