c# - .NET Core API REST C# List into List is null

标签 c# asp.net-core asp.net-web-api .net-5

我正在 net core 中开发一个 api。

我已经完成了一个 post 函数,在该函数中我发送了一个包含多个参数的对象和另一个列表中的一个列表。

当我调试代码时,函数被正确调用,但我发现第二个列表总是到达 null。

其余数据正确到达您的手中。我对其他对象进行了不同的测试,一切正常。

在这种情况下,另一个列表中的第二个到达 null。

我的代码:

示例请求输入

{
  "Name": "TestName",
  "Related1": 
   [{
      "id1": "TestNameRelated1",
      "Related2": 
      [{
         "id2": "TestNameRelated2"
      }]
   }]
}    
[HttpPost]
public resultExample Test([FromBody]TestClass test)
{
   //do something
}    
[DataContract]
public class TestClass 
{    
   [DataMember]
   public string Name { get; set; }
   [DataMember]
   public List<TestClassArray> Related1 { get; set; }
}

[DataContract]
public class TestClassArray
{    
   [DataMember]
   public string id1 { get; set; }
   [DataMember]
   public List<TestClassArray2> Related2 { get; set; }
}

[DataContract]
public class TestClassArray2
{    
   [DataMember]
   public string id2 { get; set; }
}    

这个 api 以前是在 .NET Framework 4.8 中制作的,这个案例工作正常。

现在我将 api 传递给 .Net5。

会不会是.Net5不允许在其他列表中传递列表?

您是否必须启用某种配置才能现在执行此操作?

最佳答案

您需要将类/DTO 与构造函数一起使用,如下所示,您应该可以开始了。我已经在我的 GitHub here 上上传了这个使用 .net5.0 的示例 API 应用程序代码。 .

public class TestClass
    {
        public TestClass()
        {
            Related1 = new List<TestClassArray>();
        }

        public string Name { get; set; }

        public List<TestClassArray> Related1 { get; set; }
    }

public class TestClassArray
    {
        public TestClassArray()
        {
            Related2    = new List<TestClassArray2>();
        }
    
        public string id1 { get; set; }
        
        public List<TestClassArray2> Related2 { get; set; }
    }

public class TestClassArray2
    {
        
        public string id2 { get; set; }
    }

 public class ResultExample
    {
        public string StatusCode { get; set; }
        public string Message { get; set; }
    }

Controller Post Method

        [HttpPost]
        [ProducesResponseType(typeof(ResultExample), 200)]
        public ResultExample Post([FromBody] TestClass test)
        {
            ResultExample testResult = new ResultExample();    

            TestClass test2 = new TestClass();
            TestClassArray testClassArray = new TestClassArray();
            TestClassArray2 testClassArray2 = new TestClassArray2();
            
            test2.Name = test.Name;            
            
            foreach (var item in test.Related1)
            {
                foreach (var item2 in item.Related2)
                {
                    testClassArray2.id2 = item2.id2;
                }
                
                testClassArray.Related2.Add(testClassArray2); 
            }

            test2.Related1.Add(testClassArray);

            Console.WriteLine(test2);


            testResult.Message = "New Result added successfullly....";
            testResult.StatusCode = "201";

            return testResult;
        }

Swagger Input Sample Payload

enter image description here

Post Controller Result

enter image description here

Response of Sample input payload,(You can change it to default 201 response code as well)

enter image description here

关于c# - .NET Core API REST C# List into List is null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68179985/

相关文章:

c# - 如何为应用程序和迁移使用单独的连接字符串?

c# - 获取 List<T> 的现有实例

c# - SQL 插入导致空字段

reactjs - 授权代码流如何在单页应用程序中工作?

c# - Microsoft.AspNetCore.App - 版本控制/是否应该在非 ASP.NET 类库中引用它?

asp.net-web-api - 直接从Web API核心端点返回base64编码的图像

c# - JsonSerializer.Deserialize 无法推断用法

c# - 如何在asp.net core中从客户端调用web api方法?

c# - 如何将任意 json 对象发布到 webapi

asp.net - WebApi 2 中 OData Controller http 请求的完整生命周期是什么