表单项过多时出现 HTTP 400

HTTP 400 When Too Many Form Items

我有一个 Asp.Net Core 5 Razor Pages。有一个 Razor 页面,其中包含用户用来编辑发票的表单。当表单中的项目数量显着增加(大约 500 个项目)时,应用程序 returns 出现 HTTP 400 错误。以下是我如何处理项目和表格的总结:

页面模型

[DisableRequestSizeLimit]
public class EditModel : PageModel
{
    [BindProperty]
    public OrderCreateViewModel Input { get; set; }
    
    //...rest of the code
}

视图模型

public class OrderCreateViewModel
{
    public int Id { get; set; }
    public int CustomerId { get; set; }
    public string ReceiverName { get; set; }
    public string Address { get; set; }
    public string PostalCode { get; set; }
    public string Phone { get; set; }
    public string DeliveryDate { get; set; }
    public string CustomerName { get; set; }
    //... other fields

    public int[] ItemIds { get; set; }
    public decimal[] Quantities { get; set; }
    public decimal[] Fees { get; set; }
    public decimal[] Discounts { get; set; }
    public decimal[] Taxes { get; set; }
    public decimal[] Duties { get; set; }
    public string[] ItemComments { get; set; }
    public decimal[] Max { get; set; }
}

剃须刀

<form method="post" enctype="multipart/form-data">
    <input type="hidden" asp-for="Input.Id" />
    <label asp-for="Input.CustomerName"></label>
    <input asp-for="Input.CustomerName" disabled class="form-control" />
    ...

    <table class="table mt-5 w-100 table-bordered table-responsive" id="quotation-items">
        <thead>
            <tr>
                <td>Quantity</td>
                <td>Fee</td>
                ... rest of items...
            </tr>
        </thead>
        <tbody>
            @for (int i = 0; i < Model.Input.ItemIds.Length; i++)
            {
                <tr>
                    <td>
                        <input type="text" name="Input.Quantities[]" value="@Model.Input.Quantities[i].ToString("0.##")" />
                    </td>
                    <td>
                        <input type="text" name="Input.Fees[]" value="@Model.Input.Fees[i].ToString("0.##")" />
                    </td>
                    ... rest of items ...
                </tr>
            }
        </tbody>
    </table>

    <button class="btn btn-primary btn-lg" id="btn-submit">
        Save
    </button>
</form>

如何解决这个问题?我已经将 [DisableRequestSizeLimit] 属性添加到页面模型,但它不起作用。我还尝试通过更改 web.config 文件来增加限制:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
        <httpRuntime maxRequestLength="500000000"  executionTimeout="120" />
    </system.web>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath=".\Project.exe" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
<security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="1073741824" />
      </requestFiltering>
    </security>
    </system.webServer>
  </location>
</configuration>

编辑 1:

我做了一些调试。出于某种原因,如果项目的数量恰好是 144 个项目,则该表单有效。我尝试了不同的项目。

请将其添加到 Startup#ConfigureServices

services.Configure<FormOptions>(options => options.ValueCountLimit = 5000);

这会将您的限制增加到 5000。