我在路由到我BaseController
定义 extra[ODataRoute]
的 for 控制器时遇到问题。
- 获取~/odata/Foos WORKS
- 获取 ~/odata/Foos(1)工作
- 获取 ~/odata/Foos(1)/Bars WORKS
- GET ~/odata/Bars 404 未找到
基本控制器
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);
}
}
控制器
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);
}
}
此外,我尝试采用约定方式。但这也行不通。
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);
}
}