1

I'm trying to include relational data with my Company model. When I'm not using Include, I get my answer but when I'm not including I get Could not get any response in Postman.

public async Task<IActionResult> Get(string with, string orderBy)
{
   CustContext context = new CustContext();

   var companies = context.Companies.Include(c => c.Stores).ToListAsync();

   return Ok(companies);
}

I want to return the answer in json format, which it handles by it self when not including.

Someone having a clue of what's not going well?

EDIT

When I'm debugging I see that companies is set properly together with Stores. There must be something wrong when returning the result.

ekad
  • 14,436
  • 26
  • 44
  • 46
Rikard Olsson
  • 841
  • 1
  • 7
  • 28
  • Why not to return `Task`? – dcg Feb 28 '17 at 20:03
  • I like using the `Ok()` function. Is there a nice way of setting the status code when returning `Task`? – Rikard Olsson Feb 28 '17 at 20:08
  • I believe that the status code is set properly when the method reurns, isn't it? – dcg Feb 28 '17 at 20:11
  • No sometimes you'd like a BadRequest or NotAuthorized etc. Default is 200 OK – Rikard Olsson Feb 28 '17 at 20:13
  • take a look at [this](http://stackoverflow.com/questions/17183004/return-mvc-jsonresult-plus-set-response-statuscode) question, maybe it suits for you problem. – dcg Feb 28 '17 at 20:19
  • Yeah, although its a bit off the subject. There's no problem regarding the status code - I'm not getting any answer at all, with or without returning as a JsonResult :( – Rikard Olsson Feb 28 '17 at 20:21

1 Answers1

3

I found the solution. Apparently we need to explicit tell the framework not to keep including (until infinity and beyond). So, this has to be added in startup.cs

services.AddMvc().AddJsonOptions(options =>
{
    options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
});
Rikard Olsson
  • 841
  • 1
  • 7
  • 28