Enabling OData through WebAPI

Here’s a very quick bit of code which Christain Weyer’s talk leads me to believe gives you easy OData support. OData support is enabled by (1) calling .EnableQuerySupport() on the HttpConfiguration object you get in global.asax.cs, and (2) Returning IQueryable<T> from the .Get() method of your ApiController subclass, and (3) marking that method up as [Queryable]. Something like;


// in Global.asax.cs
config.EnableQuerySupport();

// in your Api Controller
class ToDoController: ApiController
{
[Queryable]
public IQueryable Get()
{
return this.repo.Todos;
}
}

Not sure about the details, or whether this is perfect code — it’s copied out of a desperate scribbling session during the talk — but this might be enough for someone to start searching down the right track.

Advertisements