【问题标题】:Max Request Form Size Issue in ASP.NET Core MVC 3ASP.NET Core MVC 3 中的最大请求表单大小问题
【发布时间】:2019-10-04 05:43:53
【问题描述】:

我刚刚将一个 Web 应用程序从 ASP.NET Core 2.1 移植到 3.0,我收到了表单密钥长度错误:

InvalidDataException:超出表单键长度限制 2048 或值长度限制 2147483647。 Microsoft.AspNetCore.WebUtilities.FormPipeReader.ThrowKeyOrValueTooLargeException()

在我的startup.cs 中,我有以下 sn-p:

services.AddMvc()
    .SetCompatibilityVersion( CompatibilityVersion.Version_3_0 )
    .AddSessionStateTempDataProvider()
      // Maintain property names during serialization. See:
      // https://github.com/aspnet/Announcements/issues/194
      .AddNewtonsoftJson( options =>
      {
        options.SerializerSettings.ContractResolver = new DefaultContractResolver();
        options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
      } );


  // maxout form sizes
  services.Configure<FormOptions>( options =>
  {
    options.ValueCountLimit = int.MaxValue;
    options.ValueLengthLimit = int.MaxValue;
    options.MultipartHeadersLengthLimit = int.MaxValue;
  } );

但是,和过去一样,这似乎不再起作用了。也许我的表单选项需要放置的位置错误?

如果可能的话,我宁愿不装饰 100 个控件的动作;我宁愿在全局范围内设置它。

【问题讨论】:

  • 你能在这被抛出之前找出代码行吗?
  • 你的意思是在 Startup.cs 中配置 FormOptions 后仍然出现错误吗?我无法重现该问题,您能否分享一个可以重现您的问题的演示?关于表单大小的默认设置,可以参考FormReader类的source code

标签: c# asp.net-core-mvc asp.net-core-3.0


【解决方案1】:

尝试为 KeyLengthLimit 添加一个附加选项,为我修复它。

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

【讨论】:

    猜你喜欢
    • 2012-08-30
    • 1970-01-01
    • 2021-10-14
    • 1970-01-01
    • 2016-05-04
    • 2020-01-06
    • 1970-01-01
    • 2016-12-14
    • 2019-04-08
    相关资源
    最近更新 更多