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.

6
  • Why not to return Task<JsonResult>? Commented Feb 28, 2017 at 20:03
  • I like using the Ok() function. Is there a nice way of setting the status code when returning Task<JsonResult>? Commented Feb 28, 2017 at 20:08
  • I believe that the status code is set properly when the method reurns, isn't it? Commented Feb 28, 2017 at 20:11
  • No sometimes you'd like a BadRequest or NotAuthorized etc. Default is 200 OK Commented Feb 28, 2017 at 20:13
  • take a look at this question, maybe it suits for you problem. Commented Feb 28, 2017 at 20:19

1 Answer 1

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;
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.