You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AJAX向ASP.NET Core控制器传大参数时接收为null的问题排查

问题描述

通过AJAX调用ASP.NET Core控制器传递对象列表时,短列表(如8个对象)能被fundFindingsGridRows参数正常接收,但列表较长时该参数变为null。已在Startup.cs中配置多项参数但未生效,需解决大数据量接收的配置问题,同时排查是否存在其他导致参数为空的原因。


相关代码片段

Startup.cs 配置

services.AddMvc(options =>
{
    options.MaxModelBindingCollectionSize = 100000;
});

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

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

JavaScript AJAX 代码

var DATA = new Array();
var grid = $("#V3FundFindingsByBuildingGrid").data("kendoGrid");
var dataTable = grid.dataSource;

$.each(grid.items(), function (index, item) {
    var id = $(item).data('uid');
    var dataItem = dataTable.getByUid(id);
    var building = {};

    building.PANumber = dataItem.PANumber,
    building.employerNo = dataItem.employerNo,
    building.billToEntityNo = dataItem.billToEntityNo,
    building.accountNo = dataItem.AccountNo,
    building.revisionDateExists = @Model.revisionDateExists.ToString().ToLower(),
    building.settlement = false,
    building.health  = dataItem.Health,
    building.pension = dataItem.Pension,
    building.annuity = dataItem.Annuity,
    building.legal = dataItem.Legal,
    building.training = dataItem.Training,
    building.joint = dataItem.Joint,
    building.four01k  = dataItem.Four01k,
    building.healthInterest = dataItem.HealthInterest,
    building.pensionInterest = dataItem.PensionInterest,
    building.annuityInterest = dataItem.AnnuityInterest,
    building.legalInterest = dataItem.LegalInterest,
    building.trainingInterest = dataItem.TrainingInterest,
    building.jointInterest = dataItem.JointInterest,
    building.four01kInterest  = dataItem.Four01kInterest

    DATA.push(building);
});

var fundFindingsGridRows = JSON.stringify(DATA);

$.ajax({
    type: "POST",
    url: "/PayrollAudit/SaveFundFindings",
    data: fundFindingsGridRows,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (response) {
        $('#FindingsByBuildingDiv').html(response);
    },
    failure: function (response) {
        alert(response.responseText);
    },
    error: function (response) {
        alert(response.responseText);
    }
});

控制器方法

[RequestSizeLimit(100_000_000)]    
public IActionResult SaveFundFindings([FromBody]List<FundFindingsGridRow> fundFindingsGridRows)
{...}

请求信息

  • 请求头:Content-Type为application/json; charset=utf-8,请求方式为POST
  • 请求负载:JSON格式的对象列表(片段结构正常)

解决方案

1. 补充JSON序列化深度与大小配置

当前配置未覆盖JSON序列化的限制,需在AddMvc/AddControllers中添加JSON选项:

services.AddMvc(options =>
{
    options.MaxModelBindingCollectionSize = 100000;
})
.AddJsonOptions(options =>
{
    // 针对System.Text.Json(ASP.NET Core 3.0+默认)
    options.JsonSerializerOptions.MaxDepth = 64; // 默认32,可根据需要调整
    // 若使用Newtonsoft.Json(需单独安装包)
    // options.SerializerSettings.MaxDepth = 64;
    // options.SerializerSettings.MaxJsonLength = int.MaxValue;
});

2. 配置Kestrel服务器请求限制

如果项目使用Kestrel而非IIS,需单独设置Kestrel的最大请求体大小:

services.Configure<KestrelServerOptions>(options =>
{
    options.Limits.MaxRequestBodySize = 100_000_000; // 或int.MaxValue,根据实际需求设置
});

3. 排查模型绑定失败的非大小因素

长列表参数为空不一定是容量问题,可能是绑定规则不匹配:

  • 检查FundFindingsGridRow类的属性名与前端JSON键大小写完全一致(C#默认区分大小写,比如前端employerNo对应后端属性需为public string employerNo { get; set; },而非EmployerNo)
  • 验证列表中所有对象的属性值类型是否匹配后端定义(如日期格式、数值范围等,单个属性绑定失败可能导致整个列表绑定为null)
  • 启用模型绑定日志定位错误:在appsettings.json中添加日志配置,运行后查看Debug级别的绑定日志:
{
  "Logging": {
    "LogLevel": {
      "Microsoft.AspNetCore.Mvc.ModelBinding": "Debug"
    }
  }
}

4. 确认请求体完整性

用浏览器开发者工具或抓包工具检查长列表的请求体是否完整发送,排除浏览器、代理服务器截断请求的可能。


内容的提问来源于stack exchange,提问作者RoastBeast

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 17:24:16