0
votes

I have a breeze controller that is returning different JSON than a regular ole APIController.

My Breeze.js controller has a method that looks like this:

[HttpGet]
public IQueryable<Application> Applications()
{
var admin = _contextProvider.Context.Administrators.Include(i => i.Applications).Single(o => o.Name == User.Identity.Name);
    return admin.Applications.AsQueryable();
}

I have a traditional ApplicationsController: ApiController that has a method like this:

    public IEnumerable<Application> Get()
    {
        var admin = myDbContext.Administrators.Include(i => i.Applications).Single(o => o.Name == User.Identity.Name);
        return admin.Applications.AsQueryable();
    }

Basically, the code is identical. However, the response body values are not. The Breeze response body contains {$ref: "3"},{$ref: "4"} whereas the traditional WebAPI controller response is showing the proper object values.

Thoughts?

Dan

2

2 Answers

0
votes

The [BreezeController] attribute on your ApiController changes the default JSON.net serialization settings so that breeze can serialize entity graphs without repeating the same entity multiple times. The default serializer does not do this. If you apply the [BreezeController] attribute to your api controller you should see the '$ref' values show up.

0
votes

I was able to fix my issue by rewriting the LINQ to the following:

        return from u in context.Administrators
               where u.Name == administratorName
               from d in u.Applications
               select d;

I reread the documentation provided by Breeze and they emphasize not returning circular references. For those reading this, make sure your DbContext has the following in its constructor.

        Configuration.ProxyCreationEnabled = false;
        Configuration.LazyLoadingEnabled = false;

I also removed any "virtual" navigation properties.

My original LINQ had an Include statement that would return circular references. Before using a BreezeController, the standard APIController returned the data to the client accurately, just with repeated data. A BreezeController is tripped up by the circular reference and the data returned was just...well...weird and wrong.