Home > other >  ASP.Net Core 6 API Get Null Value after Successfully Post a JSON
ASP.Net Core 6 API Get Null Value after Successfully Post a JSON

Time:01-27

I just followed MS ASP.NET Core Web API Tutorial and had some issues. After created an API and can successfully POST a json format string to this API, I would like to customize its application.

The TodoItem doesn't not just contain primitive properties, also contain a List of Items.

public class TodoItem
{
    public long Id { get; set; }
    public string? Name { get; set; }
    public bool IsComplete { get; set; }
    public List<Item> TestArray { get; set; }
    public string? Secret { get; set; }
}

public class Item
{
    public long Id { get; set; }
    public string? Path { get; set; }
}

public class TodoItemDTO
{
    public long Id { get; set; }
    public string? Name { get; set; }
    public bool IsComplete { get; set; }
    public List<Item> TestArray { get; set; }
}

And here is my controller's POST and GET actions

[HttpPost]
public async Task<ActionResult<TodoItemDTO>> PostTodoItem(TodoItemDTO todoItemDTO)
{
    var todoItem = new TodoItem
    {
        IsComplete = todoItemDTO.IsComplete,
        Name = todoItemDTO.Name,
        TestArray = todoItemDTO.TestArray
    };

    _context.TodoItems.Add(todoItem);
    await _context.SaveChangesAsync();
    //return CreatedAtAction("GetTodoItem", new { id = todoItem.Id }, todoItem);
    return CreatedAtAction(
        nameof(GetTodoItem), 
        new { id = todoItem.Id }, 
        ItemToDTO(todoItem));
}
        
[HttpGet]
public async Task<ActionResult<IEnumerable<TodoItemDTO>>> GetTodoItems()
{
    return await _context.TodoItems.Select(x => ItemToDTO(x)).ToListAsync();
}

private static TodoItemDTO ItemToDTO(TodoItem todoItem) =>
    new TodoItemDTO
    {
        Id = todoItem.Id,
        Name = todoItem.Name,
        IsComplete = todoItem.IsComplete,
        TestArray = todoItem.TestArray
    };

POST request can successfully work, however, whenever I sent GET request to my localhost, I always received a null TestArray. But other properties are ok! I'm not sure if the JSON content be successfully stored in the database or not, is there any mistake I did?

POST response

HTTP/1.1 201 Created
Content-Type: application/json; charset=utf-8
Date: Thu, 20 Jan 2022 09:22:20 GMT
Location: https://localhost:7261/api/TodoItems/1
Server: Kestrel
Transfer-Encoding: chunked

{
  "id": 1,
  "name": "walk dog",
  "isComplete": true,
  "testArray": [
    {
      "id": 1,
      "path": "a"
    },
    {
      "id": 2,
      "path": "c"
    }
  ]
}

GET response

HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Date: Thu, 20 Jan 2022 09:22:23 GMT
Server: Kestrel
Transfer-Encoding: chunked

[
  {
    "id": 1,
    "name": "walk dog",
    "isComplete": true,
    "testArray": null
  }
]

CodePudding user response:

As @Peter Csala mentioned, I can successfully GET complete Json result just add Include method after TodoItems as below. TodoItems.Include(ti => ti. TestArray).Select(x => ItemToDTO(x))

  •  Tags:  
  • Related