C# ASP.NET Core MVC 网站有时会收到 NULL 而不是 POSTED 值

问题描述 投票:0回答:1

我有一个 C# ASP.NET Core MVC 网站。我在 Visual Studio 中运行它。我正在发布一个采用参数 string[] 标识符的控制器操作。 HTML 表单元素是一个文本区域,其值如下:

one
two
three

这工作正常,并且

identifiers
正确填充了 3 个条目。

如果文本区域有 37,000 多行(作为文本文件保存到磁盘时为 1.4 MB),则控制器会将

identifiers
视为 NULL。

在我的

Startup.cs
中,我使用过(并且也仅使用过默认值):

services.Configure<IISServerOptions>(o =>
{
    o.MaxRequestBodySize = int.MaxValue;
});

services.Configure<KestrelServerOptions>(o =>
{
    o.Limits.MaxRequestBodySize = int.MaxValue;
});

services.Configure<FormOptions>(o =>
{
    o.ValueLengthLimit = int.MaxValue;
    o.MultipartBodyLengthLimit = int.MaxValue;
});

我的

web.config
(我从 Visual Studio 运行它,并且
web.config
设置为“始终复制”)包含:

<configuration>
  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="1073741824" />
      </requestFiltering>
    </security>
</configuration>

Windows 事件查看器中没有错误或警告,所以我怀疑这是代码问题?

假设问题与最大 POST 大小有关,我哪里出了问题?

更新 数据使用 jQuery ajax 发布,未指定 contentType。我认为默认是: 'application/x-www-form-urlencoded;字符集=UTF-8' 这可能会将请求限制为 2k。虽然显式设置 contentType 似乎没有帮助。

c# asp.net-core-mvc visual-studio-2022 iis-express
1个回答
0
投票

要解决此问题,我必须将以下内容添加到我的控制器中:

[RequestFormLimits(ValueCountLimit = int.MaxValue)]

我原以为下面的代码会做同样的事情,但它并没有解决问题。请注意,

services
是 IServiceCollection 的一个实例,在我的 Startup.cs 中使用(我们还注入设置、数据库连接、设置会话详细信息等)。

services.Configure<FormOptions>(options =>
{
    options.ValueCountLimit = int.MaxValue;
    options.ValueLengthLimit = int.MaxValue;
});
© www.soinside.com 2019 - 2024. All rights reserved.