Back in March I wrote a quick howto on supporting $expand for entities in ASP.Net Web API. That method used an extension method to pull the $expand clause out of the query string an apply it to the ObjectSet.
I’ve now added to that sample an ActionFilter which will do this for you without any code in your controller.
To use it, change your global.asax and add the ActionFilter as part of your configuration.
public static void RegisterRoutes(RouteCollection routes) {
...
...
HttpConfiguration configuration = GlobalConfiguration.Configuration;
configuration.Filters.Add(new EFExpandActionFilter());
...
...
...
}
Now your api methods need only return an ObjectSet or ObjectQuery without any extension methods.
public IQueryable<Customer> GetCustomers() {
return northwindEntities.Customers;
}
Note that if you are using the default XML serializer you will find that the navigation properties do not serialize due to the XmlIgnore attribute, either customize your entities or use Json.Net (as in the sample project)





Pingback Supporting oData expands for EF includes in ASP.Net Web API | PeteGoo
Am I missing something? In your GitHub code, the following line in EFExpandActionFilter fails to compile since the Result property does not exist in HttpActionExecutedContext:
actionExecutedContext.Result.TryGetObjectValue(out responseObject);
I have installed the MVC 4 RC.
It’s possible something changed in the RC. I’ll try to have a look when I get some time.
I’ve updated the github source to work with the latest RC bits. Let me know how it goes.
Pete
It looks like they remove odata support from the repo. http://aspnetwebstack.codeplex.com/sourcecontrol/changeset/af11adf6b3c5?ProjectName=aspnetwebstack . Does your filter still work?
Not sure if it would still work. We’ll have to wait and see if the OData Library effort becomes usable in WebAPI. Sad news though that they decided to pull the feature