I have created a teams web api controller and trying to call the GET method to get the json result of all the teams in the database. But when I make the call I am only getti
It is worth noting that if you do control the json output like with inline JsonSerializerSettings
option,
[HttpGet]
public async Task<IActionResult> Get([FromForm]bool strip_nulls = true)
{
var teams = await _context.Teams.Include(t => t.Games).ToListAsync();
return Json(teams, new JsonSerializerSettings() {
NullValueHandling = strip_nulls ? NullValueHandling.Ignore : NullValueHandling.Include
});
}
Simply putting the suggested solution from @adeam-caglin, which is not wrong in approach, will not work. You must also set the setting in your return. For example.
[HttpGet]
public async Task<IActionResult> Get([FromForm]bool strip_nulls = true)
{
var teams = await _context.Teams.Include(t => t.Games).ToListAsync();
return Json(teams, new JsonSerializerSettings() {
NullValueHandling = strip_nulls ? NullValueHandling.Ignore : NullValueHandling.Include,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
}
It basically nulls out, not adds to the settings you set on the Startup.cs
. This also gives you a road map to not globally alter your output but do it case by case.
I would also like to take a moment and clarify what happens when you use ReferenceLoopHandling.Ignore
, you are asking to drink from the fire hose but hoping it will be a controlled flow. If you have a highly developed modeling, you will more then likely have a set where you think you going to get your intended entity and it's child list, but if those list items also have children, or other parents then you will load those. Lets say you have
Teams>Players>Contacts
Games>Teams
This would produce a heck of a json nested return. I would have been wanting a flat Game>Teams
but would end up with Games>Teams>Players
. This is a simple example but it is easy to see how you could go from a couple KB of data to never ending loop that chokes out the client consuming the results.
This is means you will need to control that flow your self. To get that flatter json return expected you will also need to incorporate .AsNoTracking()
on the .Include(x => x.Games)
As a very simple example, you would need to do something like:
[HttpGet]
public async Task<IActionResult> Get([FromForm]bool strip_nulls = true)
{
var teams = _context.Teams.AsQueryable();
teams = teams.Include(t => t.Games).AsNoTracking();
Teams _return = await teams.ToListAsync();
return Json(_return, new JsonSerializerSettings() {
NullValueHandling = strip_nulls ? NullValueHandling.Ignore : NullValueHandling.Include,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
}
Add this to Startup.cs
inside the public void ConfigureServices(IServiceCollection services)
method:
services.AddMvc().AddJsonOptions(options => {
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});
The issue was discussed https://github.com/aspnet/Mvc/issues/4160 and https://github.com/aspnet/EntityFramework/issues/4646 also see circular reference