Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Jan 27, 2013 07:54 AM by preishuber
Jul 20, 2012 09:44 PM|LINK
Around the internet, I've seen a lot of examples of people implementing oData querying with ASP.Net Web API controllers. Everyone seems to suggest that all you have to do is return an IQueryable<T> and the querying magically works, but I'm having trouble
getting it to function as described.
I have a simple controller action:
public class AdminController : ApiController
public IQueryable<Dto> Data()
var dataAdapter = new DataAdapter();
var result = dataAdapter.GetData().Select(x => new Dto(x));
No matter what oData filtering I try to apply to the URL, I get back a DTO for every record in the database. I tried just adding ?$top=1, but it still returns everything instead of selecting the top 1. Is there anything else I need to change to enable oData
filtering to work?
Other notes if they matter: This is a self-hosted service using the current nightly builds. It's not in IIS
Jul 20, 2012 09:46 PM|LINK
asked earlier today.
Jul 23, 2012 09:35 AM|LINK
Just replace this from : return result; => return result.AsQueryable();
Hope this helps.
Please mark this if it helps
Jul 23, 2012 12:55 PM|LINK
I asked the same question earlier. Apparently OData support is disabled by default in the RC. In addition to returning an IQueryable from your API function, you also need to decorate the method with the [Queryable] attribute to turn OData support on. Here
is a link to the reply I got: http://forums.asp.net/post/5075813.aspx . I confirmed that this worked in my code.
Jul 23, 2012 01:20 PM|LINK
Thanks to both of you for the help. I thought I vaguely remembered something like this being mentioned somewhere, but I can't get the [Queryable] attribute to work either:
Error 5 The type or namespace name 'QueryableAttribute' could not be found (are you missing a using directive or an assembly reference?)
I can see from the
documentation that the attribute is supposed to be declared in the System.Web.Http.dll, which I obviously have since ApiController is declared in the same namespace. The compiler finds ApiController, but it is not able to find any QueryableAttribute.
Jul 23, 2012 01:29 PM|LINK
Are you running beta or RC? the Queryable-Attribute was introduced in RC.
Jul 23, 2012 01:36 PM|LINK
I am using the nightly builds from the MVC myget package source (http://www.myget.org/F/aspnetwebstacknightly/). It's currently on version 4.0.20723.0, which was just made this morning.
Jul 23, 2012 02:42 PM|LINK
So [Queryable] was added in RC, then they took it out :)
The OData support won't be in RTM -- rather it will be in an addon package from NuGet.
Jul 23, 2012 03:10 PM|LINK
Ah, that makes more sense! I was starting to think I was losing my mind!
Do you happen to know if the oData package exists yet? I understand that part of this is the risk of using a nightly build, but it seems a little bit silly for the latest build to have less available functionality than the RC. There are some features
I need which weren't in the RC, so I am using the latest builds and hopefully reducing the hit on maintenance we will have to do when Web API RTMs. Going back to the RC isn't really a good option, but I didn't anticipate core features being removed from the
Thanks again, Brock.
Jul 23, 2012 08:49 PM|LINK
I don't think the OData addon is released in any way yet (maybe the code's in there somewhere on codeplex -- i don't know for certain). But I know when WebAPI is RTM'd in the near future a preview of the new OData stuff should also be available.