1
votes

I've got an Angular 6 application that makes calls to Web API controllers that use EF Core. By default, EF Core will not load foreign entities (lazy loading maybe?), so following suggestions, I used the following code to include such entities:

var members = await _context.Person.Include(x => x.PersonNavigation).Where(x => x.DepartmentId == id).ToListAsync();

The problem is, when the controller returns members, it sends a header of:

Transfer-Encoding: chunked

Making the result/data unreadable by my Angular application (to be expected I guess, since the response is chunked).

Any suggestions? Is this standard behavior by Web API? What can I do to return a viable JSON?

Thanks


Edit #1, per johnny 5 request:

API Controller:

public async Task<IActionResult> GetDepartmentMembers([FromRoute] int id)
    {
        var members = await _context.Person.Include(x => x.PersonNavigation).Where(x => x.DepartmentId == id).ToListAsync();
        if (members == null) { return NotFound(); }
        return Ok(members);
    }

Angular Service:

getDepartmentMembers(departmentId: number): Observable<Person[]> {
return this.http.get<Person[]>('http://localhost:49659/api/departments/' + departmentId + '/members');
}

Angular Component:

getDepartmentMembers(departmentId: number) {
this.departmentService.getDepartmentMembers(departmentId).subscribe(data => {
  this.departmentMembers = data;
});
}

Response Headers:

enter image description here

1
Huh? Can you include the Code relevant e.g the Controller. and the code which that handles http request on angular's endjohnny 5
@johnny5, I've added the question to include more code. ThanksNaner
Looks like the chunked part has nothing to do with it, and that's my bad. When looking at other Web API calls, they all have that header, but some work, others don't. When I navigate to api/departments/4/members, I get some text back, but not the whole JSON, thus, the problem, and also why I thought the chunked header was causing it. Suggestions?Naner
what is the exact error you're seeing?johnny 5
My guess is that it's returning NotFound and its invalid json?johnny 5

1 Answers

0
votes

Just as a guess since you haven't posed the error, you're not returning the correct json, paticularly in the case of the 404, I'm not really familiar with the way your doing this, but usually you would just return a list of Persons

I'm not even sure why you would want to 404 just because the department doesn't have personal, instead of just returning an Empty List.

Id first verify by changing you're code to look something like this:

public async Task<List<Person>> GetDepartmentMembers([FromRoute] int id)
{
    return await _context.Person.Include(x => x.PersonNavigation).Where(x => x.DepartmentId == id).ToListAsync();
}

If that works it's would appear to be how you're handing your NotFound responses, then you can modify your code to use a global error handler.

Usually I create a custom exception e.g InvalidId to handle my 404's etc

public async Task<List<Person>> GetDepartmentMembers([FromRoute] int id)
{
    var members = await _context.Person.Include(x => x.PersonNavigation).Where(x => x.DepartmentId == id).ToListAsync();
    if (members == null) { 
       throw new InvalidId();
    }
    return members;
}

Then you need to create a global error handler to handle your invalid id exception