简体   繁体   English

尚未完成对来自WebApi Core的Include的回复

[英]Not completed response with Include from WebApi Core

I have two models classes. 我有两个模型类。

Projects : 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 : 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: 我有简单的ProjectControllerGetProjects方法:

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

But from this GET , i get something that looks like this: 但是从这个GET ,我得到的东西看起来像这样:

在此输入图像描述

As you can see, tasks are cutted off. 如您所见, tasks被切断。

But when i delete Include : 但当我删除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 ? 我的问题是,如何使用.Net Core 2.1 WebApi包含Tasks Whats going on there? 那是怎么回事?

PS. PS。 i tried ToList() and IActionResult with ObjectResponse , all works the same (cutted tasks and projects). 我尝试使用ObjectResponse ToList()IActionResult ,所有工作都相同(切割的任务和项目)。 Postman cant get anything from it, thats why im using chrome to show you the case Postman不能从中得到任何东西,这就是为什么我用铬来向你展示这个案例

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 在你的Startup.cs ,一切正常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .NET Core 3.1 WebApi - 来自另一个 WebApi 的直通 JSON 响应 - .NET Core 3.1 WebApi - Passthrough JSON response from another WebApi Swagger UI无法显示来自ASP.net Core WebAPI的正确响应 - Swagger UI unable to show proper response from ASP.net Core WebAPI 无法从WebAPI获得响应 - Cannot get response from WebAPI ASP.NET Core 3.0 上 WebAPI 响应的最大长度 - Maximal size length of WebAPI response on ASP.NET Core 3.0 队列处理后ASP.NET Core WebApi返回响应 - ASP.NET Core WebApi return response after queue processing SignalR Core - 从 WebAPI 检查连接状态 - SignalR Core - Check connection state from WebAPI 如何从WebApi以正确的方式做出回应 - How to response in right way from WebApi Swashbuckle + ASP.Net Core WebApi:Swagger 文档不包含用于版本选择的 Request-Header 或 QueryParameter? - Swashbuckle + ASP.Net Core WebApi: Swagger Document does not include Request-Header or QueryParameter for version selection? .Net Core WebAPI连接重置,当我在我的控制器中“包含”时 - .Net Core WebAPI connection reset, when I have “include” in my controller 从.net Core中的webapi调用grpc服务的策略 - Strategies for calling grpc service from a webapi in .net Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM