Using Query Operators in ASP.Net Web API2

Introduction

This article explains how to use the Query Operators in Web API2 for filtering the data. For this we need to make the Web API method a Queryable method. Here we use the OData library that provides the [Queryable] attribute to the ASP.NET Web API2.

The following are the various query methods that we can use in the Web API:

  • $top=n: Here n=1,2,3,..... it returns the first n entities from the entity set.
  • $skip=n: Here n=1,2,3,4..... it skips the first n entities from the entity and returns the rest of the entities.
  • $select: It returns only the specified properties of the entity.
  • $filter: It returns only that entities that are matched with the expressions.
  • $orderedby: It returns the result in descending or ascending order. That is the Value of the properties.
  • $format: It defines that the returning data in which format such as XML or JSON.

Use the following procedure to create the Web API application.

Step 1

Create the application using the following:

  • Start Visual Studio 2013.
  • From the Start Window select "New Project".
  • Select "Installed" -> "Template" -> "Visual C#" -> "Web" -> "Visual Studio 2012" and then select "ASP.NET MVC4 Web Application".

    Select Web Application

  • Click on the "OK" button.
  • From the MVC4 project window select "Web API".

    Select Web API

  • Click on the "OK" button.

Step 2

Now add the Model Class:

  • In the Solution Explorer.
  • Right-click on the Model folder.
  • Select "Add" -> "Class".
  • Select "Installed" -> "Visual C#" and select "Class".

    Add An Model class

  • Click on the "Add" button.

Add the following simple lines of code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. namespace WebApplication15.Models  
  6. {  
  7.     public class Employee  
  8.     {  
  9.         public int id { getset; }  
  10.         public string Name { getset; }  
  11.         public string Address { getset; }  
  12.     }  
  13. } 

Step 3

Now add an API Controller to the project as in the following:

  • In the "Solution Explorer".
  • Right-click on the "Controller folder".
  • Select "Add" -> "Controller".
  • Select "API Controller" from the template.

    Select Web API2 Controller

  • Change name of the controller.

    Change Name of Controller

  • Click on the "Add" button.

Add the following code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Net.Http;  
  6. using System.Web.Http;  
  7. using WebApplication15.Models;  
  8. namespace WebApplication15.Controllers  
  9. {  
  10.     public class EmployeesController : ApiController  
  11.     {  
  12.         public IQueryable<Employee> GetEmployee()  
  13.         {  
  14.             List<Employee> list = new List<Employee>();  
  15.             list.Add(new Employee { id = 1, Name = "Name1", Address = "Address1" });  
  16.             list.Add(new Employee { id = 2, Name = "Name2", Address = "Address2" });  
  17.             list.Add(new Employee { id = 3, Name = "Name3", Address = "Address3" });  
  18.             list.Add(new Employee { id = 4, Name = "Name4", Address = "Address4" });  
  19.             list.Add(new Employee { id = 5, Name = "Name5", Address = "Address5" });  
  20.             list.Add(new Employee { id = 6, Name = "Name6", Address = "Address6" });  
  21.             return list.AsQueryable();  
  22.         }  
  23.     }  
  24. }   

Step 4

Now install the Web API2 OData 5.0.0 package using the following:

  • Go to the Tools menu.
  • Select "Library Package Manager" -> "Package Manager Console".

    Select Package Manager Console

  • In the Console window type this command: "Install-Package Microsoft.AspNet.WebApi.OData -Version 5.0.0 "

    Console Window

Now we add the [Queryable] attribute to the EmployeesController as in the following:

  1. [Queryable]  
  2. public IQueryable<Employee> GetEmployee() 

Step 5

Now execute the application and copy the URL. Now open the foddle2 tool, paste the copied URL into fiddle and navigate to the existing URL: "http://localhost:42095/api/employees". 

Get All Values

Select Top 2 record "http://localhost:42095/api/employees?$top=2".

Select top2 record

Skip the starting 2 record "http://localhost:42095/api/employees?$skip=3".

Skip starting 3 record


Similar Articles