ASP.NET Web API and RavenDb

Written on March 06, 2012

In this post I'll show you two a ways to provide a RavenDb IDocumentSession to your ASP.NET Web API Controllers.

The ActionFilterAttribute approach

First approach is to use a ActionFilterAttribute.

Make sure you're deriving from System.Web.Http.Filters.ActionFilterAttribute and not System.Web.Mvc.ActionFilterAttribute:

The RavenActionFilterAttribute relies on the DocumentStoreHolder Ayende introduced for his Raccoon Blog a while ago:

We also need a RavenController:

Finally we need to wire up our RavenActionFilter in Global.asax.cs:

Our concrete Controller may look like this:

Without ActionFilterAttribute

Don't do this right now because if the usage of HttpContext.Current.Items.

Another way is not to use an ActionFilterAttribute.
First update your Global.asax.cs:

Then add a RavenApiController:

Then implement your concrete Controller:

...we're done!