1

Below is a get method in my controller. It returns a JSON containing boolean "success', string "message" and a list. How can i query the list using OData? Generally if the return type was IQueryable then the following would work api/Category/all?$top=5 to get the top 5....But what should i do in my case?

// Get all Categories [HttpGet] [ActionName("all")] [Queryable] public HttpResponseMessage GetCategoryList() { var categoryList = this.Repository.GetCategories().AsQueryable<Category>(); return Request.CreateResponse(HttpStatusCode.OK, new ResponseMessage<IQueryable<Category>> { success = true, data = categoryList }); } public class ResponseMessage<T> where T: class { public string message; public bool success; public T data; } 
0

2 Answers 2

1

With the details that you have provided, the solution just seems to be utilizing the odata format. What i mean by that is that an ODATA provider, that supports the query syntax you mention, is not utilized here. So, you'll have to handle the paging scenario yourself.

There is an upcoming library, Microsoft ASP.NET Web API OData 0.2.0-alpha, that would provide that functionality but it is still in alpha.

You can bind your query string to action parameters by naming the action parameters the same as query string id. So, by modifying the action as GetCategoryList(int top) you'll be able to get the value of the top parameter in the parameter. You'll have to handle the scenario for the next X also.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use ODataQueryOptions<T> to solve the problem. Your code will look like:

[HttpGet] [ActionName("all")] public HttpResponseMessage GetCategoryList(ODataQueryOptions<Category> options) { var categoryList = this.Repository.GetCategories().AsQueryable<Category>(); categoryList = options.ApplyTo(categoryList) as IQueryable<Category>; return Request.CreateResponse(HttpStatusCode.OK, new ResponseMessage<IQueryable<Category>> { success = true, data = categoryList }); } 

ODataQueryOptions should work same as QueryableAttribute.

5 Comments

I installed the Microsoft ASP.NET Web API OData 0.2.0-alpha library but which package should i be referencing in my controller class to access ODataQueryOptions<T>??
System.Web.Http.OData.Query, the source code is at: aspnetwebstack.codeplex.com/SourceControl/changeset/view/…
In case 0.2.0-alpha doesn't include this change, you can get the latest nightly build of odata package from : aspnetwebstack.codeplex.com/discussions/353867
i used this query api/Category/all?$top=2 and it didnt work, the error was that the return type is not Queryable! I just wanted to know if the return type HAS to be of IQueryable type or is there any workaround?
Make sure that you don't apply [QueryableAttribute] on the action or the controller. And you must use ODataQueryOptions<T> type instead of ODataQueryOptions.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.