c# - .Net Core WebAPI 连接重置,当我的 Controller 中有 "include"时

标签 c# asp.net-core asp.net-core-webapi

我有一个使用 .Net Core 的新项目。这是一个 WebAPI 项目。我的模型有一个单独的项目。

在 WebAPI 项目中,在 Controller 中,我有这样的东西:

    // GET: api/questions
    [HttpGet]
    public IEnumerable<Question> GetQuestions()
    {
        return _context.Questions
            .Include( i => i.QuestionType );
    }

当我调用 http://localhost:55555/api/questios/ 时,它只返回第一条记录,然后是此错误消息: Recv 失败:连接已重置

如果我删除 Include 部分并只返回 _context.Questions,它工作得很好!

我的代码有什么问题?

最佳答案

我找到了答案。感谢所有提供帮助的人。

我根据 Loading related data 添加了 json 选项

If you are using ASP.NET Core, you can configure Json.NET to ignore cycles that it finds in the object graph. This is done in the ConfigureServices(...) method in Startup.cs.

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

关于c# - .Net Core WebAPI 连接重置,当我的 Controller 中有 "include"时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44322809/

相关文章:

c# - OpenIddict 授权请求未处理

c# - EF Core、OData v4、Automapper - 使用嵌套 $expand 查询时出现空引用异常

c# - Entity Framework Core 2.1 无法更新具有关系的实体

c# - c#中的相关路径

c# - 导致错误的阶乘方法

c# - 从 CheckboxList 中获取选定的项目 MVC.NET Razor

c# - 将类型名称作为参数用于类型名称集合的方法

c# - 在 net core 控制台应用程序中使用 Web 服务器进行简单路由

c# - 横截面 View 组件

ios - Swift 4.1 JWT http header 承载在服务器中返回401