0

BaseController追加の s を定義する for コントローラーへのルーティングに問題があります[ODataRoute]

  • GET ~/odata/Foos WORKS
  • GET ~/odata/Foos(1) WORKS
  • GET ~/odata/Foos(1)/Bars WORKS
  • GET ~/odata/Bars 404 が見つかりません

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);
    }
}

コントローラー

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);
    }
}
4

0 に答える 0