ASP.NET WebAPI Query String Parameters

Overview

In this article, we will see query string parameters in ASP.NET Web API. Let’s see in detail. 

Introduction

Let’s understand query string parameters with an example.

At the moment, the Get () method in our EmployeeController returns the list of employees as-



We have to modify this method in such a way that it returns employees by gender like a query string parameter as, stated below-

  1. ? gender=All (It will return all Employees),  
  2. ? gender=male (It should return only Male Employees) and   
  3. ?gender=Female. (Only Female Employees)  
We want to make this query string gender parameter optional. If we don’t specify this parameter at all, we still want the Get () method which should return the list of all employees, if the value is Male, Female or All. If the value is something else, it should return 400 Bad request.

Now, let’s switch to Visual Studio. In our Get method, we add a parameter and we will add a default value to this parameter as ALL. Thus, in case we don’t specify the value for this parameter, it’s going to use its default value as ALL.

We will also change the return type of this method as IEnumerable to HttpRepsonseMessage

Thus, our code is given below-
  1. public HttpResponseMessage Get(string gender = "All")  
  2. {  
  3.     using(TESTEntities entities = new TESTEntities()) {  
  4.         return entities.employeesDatas.ToList();  
  5.     }  
  6. }  
Inside our block, we will use switch statement, as given below-
  1. switch (gender.ToLower())  
  2. {  
  3.     case "all":  
  4.         return Request.CreateResponse(HttpStatusCode.OK, entities.employeesDatas.ToList());  
  5.     case "male":  
  6.         return Request.CreateResponse(HttpStatusCode.OK, entities.employeesDatas.Where(e => e.Gender.ToLower() == "male").ToList());  
  7.     case "female":  
  8.         return Request.CreateResponse(HttpStatusCode.OK, entities.employeesDatas.Where(e => e.Gender.ToLower() == "female").ToList());  
  9.     default:  
  10.         return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Value for gender must be All,Male or Female" + gender + " is invalid ");  
  11. }  
In our Switch case, we had written three conditions- the first is we can check with lower case strings. If the value is all, it will return all the employees. Similarly, if the value is male, it checks for where condition and it will return only males and similarly it is checked for females.

Thus, let’s build our solution and let’s issue a request from the Browser,



Notice, in the URL, we don’t have a gender string parameter at all. Thus, it should return both male and female respectively. Now, let’s include gender query string parameter, as given below-



Notice, we get only female employees. Let’s cross check with SQL Server, as given below-



Our output is perfectly ok. Now, let’s check with female parameter, as given below-



Now, let’s cross check with SQL records, as given below-



Now, we will check for all the parameters. Does it return list? 



Yes, it does. 

Conclusion - Thus, this was all about ASP.NET WebAPI query string parameters. Hope this article was helpful !!

 


Similar Articles