functions ASP.Net MVC4 RC Web-Api Odata filter not working with IQueryable




odata web api functions (4)

In ASP.net MVC4 RC's Web-api, I had a Get action defined like before:

public IQueryable<Person> Get()
    {
        var lst = ctx.GetListFromDB();
        return lst.AsQueryable();
    }

When it was I was running it before if I called a url like: /api/people?$inlinecount=allpages&$format=json&$top=50&$filter=(State+eq+'AL'+and+Zip+eq+'35242')

It would do the filtering on the objects, has something changed since the beta that would break this?


How can I return IQueyrable DTO from Webapi Get so I can use Odata filters

It does work in RC but perhaps not in RTM when it ships - not quite clear yet.

Your problem is that you are disposing your context since you are using a using block. So context get disposed before the data is retrieved.

So instead of using register your object for disposal at the end of request. Tugberk has a post here.



You have to put the [Queryable] attribute on the method to allow filtering. The release notes describing the change are here.

Update: In RTM they seem to have separated this feature into its own assembly so you have to include a reference to the ASP.NET Web API OData assembly from Microsoft. You can find the latest version on Nuget https://nuget.org/packages/Microsoft.AspNet.WebApi.OData

Update: In the latest version the Queryable attribute have been renamed to EnableQuery. For more information about changes see http://blogs.msdn.com/b/webdev/archive/2014/03/13/getting-started-with-asp-net-web-api-2-2-for-odata-v4-0.aspx