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

Web Api 2.0 ApiController无限嵌套循环请求响应问题求助

Web API 2.0 嵌套JSON请求响应处理方案

问题说明

在Web API 2.0的ApiController中处理Post请求时,需要实现支持任意数量嵌套ProductCode2的结构,要求请求与响应结构完全一致。当前存在两个问题:

  • 同一rowNested1下传入多个ProductCode2时,响应仅返回最后一个
  • 尝试传入多个rowNested1数组时,Postman报错且ApiController触发空引用异常

现有代码

ApiController 代码

using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using ABC.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace ABC.Controllers
{
    public class Hitung6Controller : ApiController
    {
        [HttpPost]
        public HttpResponseMessage Post([FromBody]Hitung7Model minta1)
        {
            var req1 = minta1.DistributorCode;
            var hasil1 = new List<Hitung7Model>();
            var hasil2 = new List<Hitung8Model>();
            if (req1 != null)
            {
                hasil1.Add(new Hitung7Model()
                {
                    rowNested1 = hasil2
                });
                hasil2.Add(new Hitung8Model()
                {
                });
            }
            return Request.CreateResponse(HttpStatusCode.OK, minta1);
        }
    }
}

Model 代码

using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;

namespace ABC.Models
{
    public class Hitung7Model
    {
        public string DistributorCode { get; set; }
        public List<Hitung8Model> rowNested1 { get; set; }
    }
    public class Hitung8Model
    {
        public string ProductCode2 { get; set; }
    }
}

问题根源

  1. 多ProductCode2仅返回最后一个:
    原Model本身支持单个rowNested1下的多个ProductCode2(因为rowNested1是List<Hitung8Model>),问题出在请求格式错误——比如未以数组格式传入多个Hitung8Model对象,而是重复赋值覆盖了之前的值。

  2. 多rowNested1数组触发空引用:
    原Model中rowNested1是单个List<Hitung8Model>,若传入多个数组结构,会与Model定义不匹配,导致Model绑定失败,minta1变为null,触发空引用异常。


解决方案

1. 按需调整Model结构

  • 需求1:单个rowNested1下支持任意数量ProductCode2:原Model无需修改,确保请求格式正确即可。
  • 需求2:支持多个rowNested1数组:修改Model的rowNested1类型为嵌套列表:
    public class Hitung7Model
    {
        public string DistributorCode { get; set; }
        // 改为嵌套列表,支持多个rowNested1数组
        public List<List<Hitung8Model>> rowNested1 { get; set; }
    }
    

2. 正确的请求格式示例

场景1:单个rowNested1下多ProductCode2(原Model适用)

{
    "DistributorCode": "D001",
    "rowNested1": [
        {"ProductCode2": "A1"},
        {"ProductCode2": "A2"},
        {"ProductCode2": "A100"}
    ]
}

场景2:多个rowNested1数组(修改后Model适用)

{
    "DistributorCode": "D001",
    "rowNested1": [
        [{"ProductCode2": "A1"}, {"ProductCode2": "A2"}],
        [{"ProductCode2": "B1"}, {"ProductCode2": "B2"}]
    ]
}

3. 简化并修复Controller代码

移除冗余代码,增加空引用检查避免异常:

[HttpPost]
public HttpResponseMessage Post([FromBody]Hitung7Model minta1)
{
    if (minta1 == null)
    {
        return Request.CreateResponse(HttpStatusCode.BadRequest, "请求格式错误,请检查JSON结构");
    }
    return Request.CreateResponse(HttpStatusCode.OK, minta1);
}

4. 确保请求配置正确

Postman中需设置请求头Content-Type: application/json,避免格式不匹配导致绑定失败。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 07:11:29