简体   繁体   中英

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

I'm developing an api in net core.

I've done a post function in which I send an object containing multiple parameters and a list within another list.

When I'm debugging the code the function is called correctly but I find that the second list always arrives null.

The rest of the data arrives at you correctly. I have done different tests with other objects and everything works correctly.

It is this case in which the list within another the second one arrives null.

My code:

example request input

{
  "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; }
}    

This api was previously made in .NET framework 4.8 and this case worked correctly.

Now I'm passing the api to.Net5.

Could it be that in.Net5 it is not allowed to pass lists within other lists?

Do you have to enable some kind of configuration to be able to do this now?

You need use class/DTO with constructor like shown below and you should be good to go. I have uploaded this sample API app's code working with .net5.0 on my GitHub here .

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

在此处输入图片说明

Post Controller Result

在此处输入图片说明

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

在此处输入图片说明

I had a similar issue.

API method shows List was null

In my case a date field was not well formatted

So I use SimpleDateFormat on Android Studio with a correct datetime format

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss",Locale.US);
item.setDate(dateFormat.format(calendar.getTime()));

and works fine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM