I have two models classes.

Projects:

public class Project
{
    public Project()
    {
    }

    [Key]
    public int Id { get; set; }

    public string Tag { get; set; }

    public string Name { get; set; }

    public string Description { get; set; }

    public virtual List<Task> Tasks { get; set; }
}

And Tasks:

public class Task
{
    public Task()
    {
    }

    [Key]
    public int Id { get; set; }

    [ForeignKey("Project")]
    public int ProjectId { get; set; }

    public Project Project { get; set; }

    public string Name { get; set; }

    public string Description { get; set; }

    public Priority Priority { get; set; }

    public int? StoryPoints { get; set; }

    public DateTime? StartTime { get; set; }

    public DateTime? EndTime { get; set; }
}

I have simple ProjectController with GetProjects method:

    [HttpGet]
    public IEnumerable<Project> GetProjects()
    {
        return context.Projects.Include(t => t.Tasks);
    }

But from this GET, i get something that looks like this:

enter image description here

As you can see, tasks are cutted off.

But when i delete Include :

    [HttpGet]
    public IEnumerable<Project> GetProjects()
    {
        return context.Projects;
    }

Everything works fine:

enter image description here

But of course, we dont get tasks.

My question is, how to get that included Tasks to project with .Net Core 2.1 WebApi? Whats going on there?

PS. i tried ToList() and IActionResult with ObjectResponse, all works the same (cutted tasks and projects). Postman cant get anything from it, thats why im using chrome to show you the case

1 Answers

0
michasaucer On

Problem solved. You need to add:

services.AddMvc().AddJsonOptions(options => 
    { options.SerializerSettings.ReferenceLoopHandling = 
                                Newtonsoft.Json.ReferenceLoopHandling.Ignore; });

in your Startup.cs and everything works fine