Search code examples
asp.net-web-apiodataasp.net-web-api-routingasp.net-web-api2asp.net-web-api-odata

How to configure odata web api route for method that always returns single entity


I'd like to configure a route that always returns a single entity.

The controller looks like:

class StatsController: ODataController { public Stats Get() { return new Stats(); } } The url to access it should be: GET ~/service-prefix/stats

All the options I've seen involve having to return IQueryable, or when returning a single entity, passing in a key in a form of ~/service-prefix/EntitySet(1)

Is there a way to achieve the above without having to return an IQueriable?


Solution

  • To access a single object without needing to have an entityset, odata v4 introduces the concept of singletons.

    From OData v4 spec:

    A singleton allows addressing a single entity directly from the entity container without having to know its key, and without requiring an entity set.

    More info: