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

匿名 (未验证) 提交于 2019-12-03 01:39:01

问题:

I have a new project with .Net Core. It's a WebAPI project. And I have a separate project for my model.

In WebAPI project, in a controller, I have something like this:

    // 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

If I remove the Include part and just return the _context.Questions, it work just fine!

What's wrong in my code?

回答1:

I've found the answer. Thank you everyone who helped.

I added json options according to Loading related data

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 ); 


回答2:

Try this:

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!