ASP.NET Core 解決 Failed to read the request form. Form value count limit 1024 exceeded

在使用表單 form-data 格式傳送請求時出現下列錯誤:
    
{
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
    "title": "One or more validation errors occurred.",
    "status": 400,
    "traceId": "00-7e30eef0999a4f5d829f0b183f7b0438-2c918bf0f47a61d7-00",
    "errors": {
        "": [
            "Failed to read the request form. Form value count limit 1024 exceeded."
        ]
    }
}
    

為了避免被過大的表單惡意攻擊,預設限制請求的表單欄位不能超過 1024 格,假設真的有這麼多欄位的需求,可以在 Program.cs 中透過調整 FormOptions 設定值來變更上線:
    
var builder = WebApplication.CreateBuilder(args);

builder.Services.Configure<FormOptions>(options => { options.ValueCountLimit = 2048; });

var app = builder.Build();
    

留言