Using OData features in a ASP.NET MVC Web API using Visual Studio 2012

In this article we will focus on how to use the OData features in a ASP.NET MVC Web API using Visual Studio 2012. We will start by a basic introduction to OData followed by using OData features in Web API.

In this article we will focus on how to use the OData features in a ASP.NET MVC Web API using Visual Studio 2012.

What is OData:
OData is a Data Access Protocol.

Why to use OData:
•It provides a uniform way to query the data, perform different operations against the data such as insert/update/delete/select
•It supports different formats such as JSON/XML.
•Using OData we can also easily expose metadata about the data which can be used by clients to discover type information and relationships between different entities.
• It provides additional features on top of the basic functionality such as filtering the records based on some condition, paging, sorting, retrieving selective number of records and many more in a very easy manner.

OData in action
We will create a simple WebAPI OData project which allows us to perform simple operations like filtering the data, selecting first few records, sorting the records by a particular column.

Step 1
Launch Visual Studio 2012 -> File -> New -> Project -> ASP.NET MVC4 Web Application -> Select Empty Template -> click Ok.
Step 2
Right click Models folder and add a new class with the name "Emp". Add Id,Name,Salary properties to this class.

public class Emp
public int ID { get; set; }
public string Name { get; set; }
public decimal Salary { get; set; }


Step 3
Right click Controller folder and add a new class with the name "EmpController". Derive this class from ApiController class and Add a Get method which returns the list of Employees. We will annotate this class with "Queryable" attribute. This attribute enables querying using the OData query syntax.

public class EmpController : ApiController
public IQueryable Get()
List lstEmp = new List();
lstEmp.Add(new Emp { ID = 1, Name = "Priya", Salary = 5000 });
lstEmp.Add(new Emp { ID = 2, Name = "Reena", Salary = 15000 });
lstEmp.Add(new Emp { ID = 3, Name = "John", Salary = 25000 });
lstEmp.Add(new Emp { ID = 4, Name = "Nita", Salary = 1000 });
lstEmp.Add(new Emp { ID = 5, Name = "Sam", Salary = 62000 });
lstEmp.Add(new Emp { ID = 6, Name = "Robert", Salary = 8000 });
lstEmp.Add(new Emp { ID = 7, Name = "Sakshi", Salary = 9000 });
lstEmp.Add(new Emp { ID = 8, Name = "Kate", Salary = 25000 });
return lstEmp.AsQueryable();

Note: If you get an error:IQueryable is not an attribute class that means OData feature is not part of your project. To enable it. Please follow below steps:
1.Open Solution Explorer -> Right Click the project -> Select Manage NuGet Packages
2.In the " Manage NuGet Packages" window search for "odata" online
3.From the search results select Microsoft ASP.NET Web API 2.1 OData
4.Click on Install and Accept the License Agreement -> click Close.
Step 4
Build the project and run the application. Copy the Url from the browser window.
Step 5
Launch Fiddler. Go to Compose tab and paste the url and append it with "/api/Emp".
For Example:http://localhost:3216/api/Emp
Click on Execute.
Step 6
Double click on result on the right side. This will display the list of Emp Details.
To get only the first two employee details from the Emp list append the url with
For Example: http://localhost:3216/api/Emp?$top=2

To orderby Name of the employee append the url with "$orderby=Name". This will order the results by name of the employee.

For Example:http://localhost:3216/api/Emp?$orderby=Name

To list Page 2 with 3 items per page ordered by ID of an employee.

To list Emp details whose name ends with letter a, use below query

Please note that if you get an error as shown below. Please follow my article here
"Attempt by security transparent method 'System.Web.Http.GlobalConfiguration.get_Configuration()' to access security critical type 'System.Web.Http.HttpConfiguration' failed".

To know more about OData conventions. Go through the below link.


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: