简体   繁体   中英

Not completed response with Include from WebApi Core

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:

在此输入图像描述

As you can see, tasks are cutted off.

But when i delete Include :

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

Everything works fine:

在此输入图像描述

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

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

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