I have a Web API Controller in my Asp.Net Core Web API 2.2 project.
Messageboard
model:
public class MessageBoard
{
public long Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public ICollection<Message> Messages { get; set; }
}
Message
model:
public class Message
{
public long Id { get; set; }
public string Text { get; set; }
public string User { get; set; }
public DateTime PostedDate { get; set; }
public long MessageBoardId { get; set; }
[ForeignKey("MessageBoardId")]
public MessageBoard MessageBoard { get; set; }
}
This is one of my Web API Controller actions, shortened for brevity:
[Route("api/[controller]")]
[ApiController]
public class MessageBoardsController : ControllerBase
{
// GET: api/MessageBoards
[HttpGet]
public async Task<ActionResult<IEnumerable<MessageBoard>>> GetMessageBoards()
{
return await _context.MessageBoards
.Include(i => i.Messages)
.ToListAsync();
}
}
Whenever I issue a GET request to MessageBoards, only part of the correct JSON is returned. Here is the returned JSON from accessing https://localhost:44384/api/MessageBoards/
on Postman:
[{"id":1,"name":"Test Board 2","description":"A 2nd Message board for testing purposes.","messages":[{"id":1,"text":"Posting my first message!","user":"Jesse","postedDate":"2019-01-01T00:00:00","messageBoardId":1
The JSON is cut-off (hence why it's an ugly block and not beautified by Postman), presumably due to the MessageBoard
property on the Message
model since it is the first missing JSON item.
How can I make the action correctly return the list of MessageBoards and child Messages?
I see you are using Eager Loading
in your query. So add the following configuration in your Startup
class to ignore cycles that it finds in the object graph and to generate JSON
response properly.
public void ConfigureServices(IServiceCollection services)
{
...
services.AddMvc()
.AddJsonOptions(
options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
);
...
}
For more details: Related data and serialization in EF Core
Try to create and return a DTO or a new struct/class that will not have cyclic info (MessageBoard has Message that has MessageBoard etc...)
来源:https://stackoverflow.com/questions/54633340/asp-net-core-web-api-2-2-controller-not-returning-complete-json