ASP.NET Web API Query String Parameter

Introduction

Query String Parameters is an important concept in .NET programming. Let us learn how to use query string parameters in ASP.NET Web API programming. Follow the steps mentioned below.

Step 1

Open SQL Server 2014 or a version of your choice, create a table, and insert some records.
  1. CREATE TABLE [dbo].[Employees](    
  2.     [EmployeeId] [int] IDENTITY(1,1) NOT NULL,    
  3.     [Name] [nvarchar](50) NULL,    
  4.     [Gender] [char](10) NULL,    
  5.     [Age] [intNULL,    
  6.     [Position] [nvarchar](50) NULL,    
  7.     [Office] [nvarchar](50) NULL,    
  8.     [HireDate] [datetime] NULL,    
  9.     [Salary] [intNULL,    
  10. PRIMARY KEY CLUSTERED     
  11. (    
  12.     [EmployeeId] ASC    
  13. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]    
  14. ON [PRIMARY]    
  15.     
  16. GO    

Step 2

Insert records into the SQL table, as shown in the following code.
  1. insert into Employees values('Airi Satou','Male',34,'Software Engineer','Tokyo','2018-11-01',162700)    
  2. insert into Employees values('Angelica Ramos','Male',40,'Accountant','London','2018-08-10',1200000)    
  3. insert into Employees values('Caesar Vance','Male',42,'Sales Assistant','New York','2018-05-01',162700)    
  4. insert into Employees values('Cara Stevens','Male',30,'Pre-Sales Support','New York','2018-11-01',162700)    
  5. insert into Employees values('Colleen Hurst','Male',34,'Software Engineer','San Francisco','2018-12-05',86000)    
  6. insert into Employees values('Dai Rios','Male',45,'Regional Director','London','2018-08-10',132000)    
  7. insert into Employees values('Gavin Joyce','Female',28,'Developer','San Francisco','2018-09-15',206850)    
  8. insert into Employees values('Brenden Wagner','Male',38,'Support Engineer','New York','2018-10-20',372000)    
  9. insert into Employees values('Cara Stevens','Female',30,'Integration Specialist','Tokyo','2018-12-25',145600)    
  10. insert into Employees values('Cedric Kelly','Female',22,'Senior Javascript Developer','Edinburgh','2018-12-30',106450)    

Step 3

Open Visual Studio 2017, click on New Project, and create an empty Web API application project.
ASP.NET Web API Query String Parameter

After clicking on the "New Project" link, a window will appear. Select "Web" from the left panel, choose ASP.NET Web application, give a meaningful name to your project, and then click OK, as shown in the below screenshot.

ASP.NET Web API Query String Parameter

After clicking on OK, one more window will appear; choose Empty, check on Empty Web API checkbox and then, click OK, as shown in the following screenshot.

ASP.NET Web API Query String Parameter

After clicking OK, the project will be created with the name you have given. In my case, it is WebAPIQueryString_Demo.

Step 4

Add Entity Framework now. For that, right-click on Models folder, select Add >> New Item, then, click on it.
ASP.NET Web API Query String Parameter

Again, you will get a window, from where you need to select "Data" from the left panel and choose ADO.NET Entity Data Model. Give it a name, like DBModel (this name is not mandatory, you can give any name) and click "Add".

ASP.NET Web API Query String Parameter

After you click on "Add a window", the wizard will open. Choose EF Designer from the database and click "Next".

ASP.NET Web API Query String Parameter

After clicking on "Next", a window will appear. Choose "New Connection". Another window will appear; add your server name here. If it is local, then enter dot (.) instead of the server name. Choose your database and click "OK".

ASP.NET Web API Query String Parameter

The connection will be added. If you wish to save the Connection, you can opt for it. You can change the name of your connection like below. It will save the connection in web.config. Click "Next".

ASP.NET Web API Query String Parameter

In the next window, choose database table name as shown in the below screenshot, and click "Finish". Entity Framework will be added to your project and the respective class gets generated under "Models" folder.

ASP.NET Web API Query String Parameter

The following class will be added.

  1. namespace WebAPIQueryString_Demo.Models  
  2. {  
  3.     using System;  
  4.     using System.Collections.Generic;  
  5.       
  6.     public partial class Employee  
  7.     {  
  8.         public int EmployeeId { get; set; }  
  9.         public string Name { get; set; }  
  10.         public string Gender { get; set; }  
  11.         public Nullable<int> Age { get; set; }  
  12.         public string Position { get; set; }  
  13.         public string Office { get; set; }  
  14.         public Nullable<System.DateTime> HireDate { get; set; }  
  15.         public Nullable<int> Salary { get; set; }  
  16.     }  
  17. }   

Step 5

Right-click on the Controllers folder, select Add >> Controller, as shown in the below screenshot.
ASP.NET Web API Query String Parameter

After clicking on Controller, a window will appear. Choose Web API 2 Controller-Empty and click "Add".

ASP.NET Web API Query String Parameter

Another window will appear with a name DefaultController. Change the name to EmployeeController and click "Add". The EmployeeController will be added under the Controllers folder. Remember, you don’t have to change the Controller suffix for all the controllers. You should change only the first part of the name, i.e., in place of Default, just change Employee, as shown in the below screenshot.

ASP.NET Web API Query String Parameter

Complete code for Controller

  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 WebAPIQueryString_Demo.Models;  
  8.   
  9. namespace WebAPIQueryString_Demo.Controllers  
  10. {  
  11.     public class EmployeeController : ApiController  
  12.     {  
  13.         private EmployeeEntities contentEntities;  
  14.   
  15.         public EmployeeController()  
  16.         {  
  17.             contentEntities=new EmployeeEntities();  
  18.         }  
  19.   
  20.         [HttpGet]  
  21.         public HttpResponseMessage GetEmployees(string gender="All")  
  22.         {  
  23.             switch (gender.ToLower())  
  24.             {  
  25.                 case "all":  
  26.                     return Request.CreateResponse(contentEntities.Employees.ToList());  
  27.                 case "male":  
  28.                     return Request.CreateResponse(contentEntities.Employees.Where(e => e.Gender == "male").ToList());  
  29.                 case "female":  
  30.                     return Request.CreateResponse(contentEntities.Employees.Where(e => e.Gender == "female").ToList());  
  31.                 default:  
  32.                     return Request.CreateErrorResponse(HttpStatusCode.BadRequest,"Value for gender must be Male,Female or All." + gender +  
  33.                                                        " is invalid.");  
  34.             }  
  35.         }  
  36.     }  
  37. }  

Step 6

Build and run the project by pressing Ctrl+F5. The following is the output. 
 
Output 

http://localhost: 53491/api/employee?gender=all

ASP.NET Web API Query String Parameter

http://localhost: 53491/api/employee?gender=male

ASP.NET Web API Query String Parameter

http://localhost:53491/api/employee?gender=abcdef

ASP.NET Web API Query String Parameter


Similar Articles