Search code examples
c#restasp.net-web-apiodata

Can OData controller contains REST methods?


I've created OData 4 controller in Web API 2 that looks like this:

public class BooksController : ODataController
{
    private ICollection<Book> books;

    public BooksController()
    {
        this.books = new Collection<Book>()
        {
            new Book() { Id = "1", Title = "B1", Price = new Money(1, "EUR") },
            new Book() { Id = "2", Title = "B2", Price = new Money(2, "EUR") }
        };
    }

    [EnableQuery]
    public IQueryable<Book> Get()
    {
        return this.books.AsQueryable();
    }
}

Problem is, when I want to create regular REST method like this one:

[HttpGet]
[Route(("api/books"))]
public IEnumerable<Book> GetAll([FromUri] PaginationDto paginationDto)
{
...
}

First method is accessible requesting /odata/Books. Response contains data according to the OData specification. Second method (should be) accessible by requesting /api/books. Response should contains standard JSON array of PLU objects.

How could I achieve this?

Either I'm able to create pure OData controller or regular REST controller (deriving from ApiController) that unfortunatelly does not comply OData standard when requesting OData route /odata/Books.


Solution

  • I came up with a solution that involves creation of a regural controller containing all the non OData methods, regular OData controller and a custom HTTP controller selector for this OData controller, because these are is posfixed with ODataController.