0

I'm having problems routing to my BaseController for controllers where I define extra [ODataRoute]s.

  • GET ~/odata/Foos WORKS
  • GET ~/odata/Foos(1) WORKS
  • GET ~/odata/Foos(1)/Bars WORKS
  • GET ~/odata/Bars 404 Not Found

BaseController

public abstract class BaseController<T> : ODataController where T : class
{
    protected IGenericRepository<T> Repository;

    public BaseController(IGenericRepository<T> repository)
    {
        Repository = repository;
    }

    [EnableQuery]
    public IHttpActionResult Get() // <---------- THIS IS THE PROBLEM
    {
        return Ok(Repository.AsQueryable());
    }

    [EnableQuery]
    public IHttpActionResult Get(int key) // WORKS
    {
        var entity = Repository.GetByKey(key);
        return Ok(entity);
    }
}

Controllers

public class FoosController : BaseController<Foo>
{
    public FoosController(IGenericRepository<Foo> repository)
        : base(repository)
    {
    }

    // Can route to base without problems
    // Both base.Get() and base.Get(1) works
}

public class BarsController : BaseController<Bar>
{
    public FoosController(IGenericRepository<Bar> repository)
        : base(repository)
    {
    }

    // Can't route to base.Get()
    // But base.Get(1) works

    // GET /Foos(1)/Bars
    [EnableQuery]
    [ODataRoute("Foos({key})/Bars")]
    public IHttpActionResult GetBars(int key) // WORKS
    {
        var result = Repository.AsQueryable().Where(x => x.FooId == key);
        return Ok(result);
    }
}

Additionally I tried going the convention way. But that doesn't work either.

public class FoosController : BaseController<Foo>
{
    public FoosController(IGenericRepository<Foo> repository)
        : base(repository)
    {
    }

    // GET /Foos(1)/Bars
    [EnableQuery]
    public IHttpActionResult GetBars(int key) // WORKS
    {
        var result = Repository.AsQueryable().Bars;
        return Ok(result);
    }
}
Snæbjørn
  • 10,322
  • 14
  • 65
  • 124

0 Answers0