简体   繁体   English

.Net Core WebAPI连接重置,当我在我的控制器中“包含”时

[英].Net Core WebAPI connection reset, when I have “include” in my controller

I have a new project with .Net Core. 我有一个带有.Net Core的新项目。 It's a WebAPI project. 这是一个WebAPI项目。 And I have a separate project for my model. 我的模型有一个单独的项目。

In WebAPI project, in a controller, I have something like this: 在WebAPI项目中,在控制器中,我有这样的东西:

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

When I call http://localhost:55555/api/questios/ it just returns the first record, and then this error message: Recv failure: Connection was reset 当我调用http://localhost:55555/api/questios/它只返回第一条记录,然后出现此错误消息:Recv failure:Connection was reset

If I remove the Include part and just return the _context.Questions , it work just fine! 如果我删除Include部分并返回_context.Questions ,它就可以了!

What's wrong in my code? 我的代码有什么问题?

I've found the answer. 我找到了答案。 Thank you everyone who helped. 谢谢所有帮助过的人。

I added json options according to 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. 如果您使用的是ASP.NET Core,则可以将Json.NET配置为忽略它在对象图中找到的循环。 This is done in the ConfigureServices(...) method in Startup.cs. 这是在Startup.cs中的ConfigureServices(...)方法中完成的。

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

Try this: 尝试这个:

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

暂无
暂无

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

相关问题 为什么我有 2 个类用于这个 .net 核心 webapi 端点? - Why do I have 2 classes for this .net core webapi endpoint? 如何在Ubuntu上托管/发布我的.Net Core WebAPI? - How do I host/publish my .Net Core WebAPI on Ubuntu? ASP.NET Core 3.0 WebAPI - 为什么在控制器方法中处理异常时我仍然收到 InternalServerError? - ASP.NET Core 3.0 WebAPI - why do I still receive InternalServerError when exception is handled in the Controller Method? 接受.net核心webapi控制器中的byte [] - Accepting byte[] in a .net core webapi controller 在.Net core 2.0中,当我向控制器发送POST请求时,数据未绑定到我使用的对象。 如何绑定这些数据? - In .Net core 2.0 when I send a POST Request to my controller the data does not bind to the object I have used. How do I bind this data? 当我在查询中使用“包含”时,ASP.NET Core Web API 返回不完整的 JSON 数据 - ASP.NET Core Web API returns incomplete JSON data when I use 'include' in my query 带有MySQL DB的.NET Core 2.0 WebAPI-dotnet发布-运行.dll时连接字符串为null - .NET Core 2.0 WebAPI with MySQL DB - dotnet publish — Connection string null when running .dll 如何在返回对象的ASP.NET Core WebAPI控制器中抛出异常? - How can I throw an exception in an ASP.NET Core WebAPI controller that returns an object? ASP.NET 核心:我想从控制器方法的视图中访问复选框列表(值) - ASP.NET Core: I want to have access to the list of checkboxes (value) from my view on my controller's method ASP.NET Core WebAPI 崩溃是因为我没有 StaticFileMiddleware? - ASP.NET Core WebAPI crashes because I don't have StaticFileMiddleware?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM