How to deal with ASP.NET Core 2.0 Chunked Web Api Call with EF Core

angular asp.net-core asp.net-core-2.0 asp.net-web-api entity-framework-core

Question

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
1
7/9/2018 8:26:09 PM

Popular Answer

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

0
7/9/2018 8:47:58 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow