[英].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.