Search code examples
c#asp.net-mvc-5odataaspnetboilerplate

OData controller not returning entities


Currently, I am implementing OData with ASP.NET Zero. I have followed the instructions in the documentation but I always get an empty response.

This is my Web API module PreInitialize code:

public override void PreInitialize()
{
    var builder = Configuration.Modules.AbpWebApiOData().ODataModelBuilder;

    // Configure your entities here...
    builder.EntitySet<Author>("Authors");
}

I created an OData controller for the Authors and overrode the Get method:

public class AuthorsController : AbpODataEntityController<Author, long>, ITransientDependency
{
    private readonly IRepository<Author, long> _repository;
    public AuthorsController(IRepository<Author, long> repository) : base(repository)
    {
        _repository = repository;
    }

    [EnableQuery]
    [UnitOfWork(IsDisabled = true)]
    public override IQueryable<Author> Get()
    {
        return _repository.GetAll();
    }

    [EnableQuery]
    public override SingleResult<Author> Get([FromODataUri] long key)
    {
        return new SingleResult<Author>(_repository.GetAll().Where(x=>x.Id==key));
    }
}

Upon navigating to the URL http://localhost:6235/odata/Authors, I am only seeing an empty array for value in the OData response:

{"@odata.context":"http://localhost:6235/odata/$metadata#Jobs","value":[]}

Solution

  • It is likely a data filter issue.

    1. Add logging to DB Context and check the query.

        // In the constructor
        Database.Log = (s) => System.Diagnostics.Debug.WriteLine(s);
      
    2. Check for user scope and custom interceptors that may be setting parameters for your data filters.