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
{
[HttpGet]
public IQueryable<Dto> Data()
{
var dataAdapter = new DataAdapter();
var result = dataAdapter.GetData().Select(x => new Dto(x));
return result;
}
}
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
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.
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.
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.
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
nightly builds.
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.
Tim Copenhav...
Member
3 Points
8 Posts
oData querying not working?
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 { [HttpGet] public IQueryable<Dto> Data() { var dataAdapter = new DataAdapter(); var result = dataAdapter.GetData().Select(x => new Dto(x)); return result; } }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
BrockAllen
All-Star
28114 Points
4997 Posts
MVP
Re: oData querying not working?
Jul 20, 2012 09:46 PM|LINK
Just asked earlier today.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
martinyap
Member
38 Points
14 Posts
Re: oData querying not working?
Jul 23, 2012 09:35 AM|LINK
Hi,
Just replace this from : return result; => return result.AsQueryable();
Hope this helps.
Please mark this if it helps
richmiller
Member
40 Points
51 Posts
Re: oData querying not working?
Jul 23, 2012 12:55 PM|LINK
Tim,
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.
Rich
Tim Copenhav...
Member
3 Points
8 Posts
Re: oData querying not working?
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.
MartinJ.
Member
249 Points
83 Posts
Re: oData querying not working?
Jul 23, 2012 01:29 PM|LINK
Are you running beta or RC? the Queryable-Attribute was introduced in RC.
Tim Copenhav...
Member
3 Points
8 Posts
Re: oData querying not working?
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.
BrockAllen
All-Star
28114 Points
4997 Posts
MVP
Re: oData querying not working?
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.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
Tim Copenhav...
Member
3 Points
8 Posts
Re: oData querying not working?
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 nightly builds.
Thanks again, Brock.
BrockAllen
All-Star
28114 Points
4997 Posts
MVP
Re: oData querying not working?
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.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/