Dot-Net
表單送出導致“InvalidDataException:超出表單值計數限制 1024”。
我創建了一個 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。”
更新: 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)
預設的**formvalue(不是 formkey)**限制為 1024。
另外,我認為您可以更改Startup.cs文件
FormOptions中的限制。public void ConfigureServices(IServiceCollection services) { services.Configure<FormOptions>(options => { options.ValueCountLimit = int.MaxValue; }); }