Bringing oData to your WebAPI – just use IQueryable

In In the previous post, I discussed how to implement an ASP.NET Web API service with ASP.NET MVC. The cool thing about the Web API is the stuff we get for free. One of the nice things we get for free is oData. Jon Galloway posted a nice how-to video here.

To illustrate how the concept applies to the examples here, consider this method on our Prouducts API:

  public IEnumerable GetProducts()
  {
   return ProductsRepository.data;
  }

Nothing all that special going on here. The method returns a list of products – in no particular order.

What if you wanted the list ordered? Or, what if you wanted a subset of the list? You might think you would have to outfit your repository with special logic handle your query parameters. Thanks to the fact that oData is baked into Web API, the work is alredy done for you.

To take advantage of oData, we have to make one minor tweak to GetProducts() method. We need to make it of the type IQueryable. Here is the revised method:

  public IQueryable GetProducts()
  {
   return ProductsRepository.data.AsQueryable();
  }

With IQueryable in place, we can take advantage of oData. If we want our product list, but in descending order of price, the following url will do the trick:

http://localhost:18950/api/products?$orderby=price desc

And here are the results:

If you want the product called Gizmo 3:

http://localhost:18950/api/products?$filter=name eq 'Gizmo 3'

And going back to our order by price descending example, if you wanted the most expensive product:

http://localhost:18950/api/products?$orderby=price desc&$top=1

The complete oData Protocol URI conventions can be found here

About johnvpetersen

I've been developing software for 20 years, starting with dBase, Clipper and FoxBase + thereafter, migrating to FoxPro and Visual FoxPro and Visual Basic. Other areas of concentration include Oracle and SQL Server - versions 6-2008. From 1995 to 2001, I was a Microsoft Visual FoxPro MVP. Today, my emphasis is on ASP MVC .NET applications. I am a current Microsoft ASP .NET MVP. Publishing In 1999, I wrote the definitive whitepaper on ADO for VFP Developers. In 2002, I wrote the Absolute Beginner’s Guide to Databases for Que Publishing. I was a co-author of Visual FoxPro Enterprise Development from Prima Publishing with Rod Paddock, Ron Talmadge and Eric Ranft. I was also a co-author of Visual Basic Web Development from Prima Publishing with Rod Paddock and Richard Campbell. Education - B.S Business Administration – Mansfield University - M.B.A. – Information Systems – Saint Joseph’s University - J.D. – Rutgers University School of Law (Camden) In 2004, I graduated from the Rutgers University School of Law with a Juris Doctor Degree. I passed the Pennsylvania and New Jersey Bar exams and was in private practice for several years – concentrating transactional and general business law (contracts, copyrights, trademarks, independent contractor agreements, NDA’s, intellectual property and mergers and acquisitions.).
This entry was posted in ASP.NET MVC 4, oData, WebAPI. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • Pingback: Blue Ray Plus - Latest Technology News

  • Radenko Zec

    Thanks for source code, but I don’t know how to download or view it… :)

  • Anonymous

    Glad you like the post. The link to the source code is in the first line of the post.

    Here’s the link: bit.ly/mvc4essentials_phillydotnet.

    In the meantime, I’ll ammend the post to include the RequestMethod class listing.

    Thanks for your comment.

    John

  • Radenko Zec

    Hi There. Nice post. Where can I find RequestMethod class ?