Implement Version in Web API: Using Query String

Welcome to the “Implement Version in Web API” article series. In our previous article we learned how to implement versions in Web API using a different controller. You can read it here.
 
Implement Version in Web API: Using Different Controller
 
In this article we will learn how to implement version information using a Query string. In our previous article we explained why versioning is needed and the various ways to implement versioning in Web API. So, we are not repeating that again here. Let’s try to understand how to implement versioning using a Query string.
 
Let’s see how the query string will look like. For example, we want to create a different version of an existing Student Information service. Now, to consume the latest version, the client must pass the version information along with the query string.
If they do not pass any value in the query then by default Version 1 will execute.  If they specify Version 1 in the query string then it will execute Version 1 and if they specify Version 2 then as expected Version 2 will execute.
 
URL for Version 1
 
http://localhost:11129/api/Student/1?v=1
 
URL for Version 2
 
http://localhost:11129/api/Student/1?v=2
 
Fine, we understand how to call a different version of the same Web API service.  Now let’s implement it practically.

Implement the Student Model class with the following. This class has two constructors and the two constructors will be called at the time of object creation for a different version.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. namespace TestWEB_API.Models  
  6. {  
  7.     [Serializable]  
  8.     public class StudentInformation  
  9.     {  
  10.         public int Id;  
  11.         public string Name;  
  12.         public string Surname;  
  13.         public string Course;  
  14.         public StudentInformation(int Id, string Name, string Surname)  
  15.         {  
  16.             this.Id = Id;  
  17.             this.Name = Name;  
  18.             this.Surname = Surname;  
  19.         }  
  20.         public StudentInformation(int Id, string Name, string Surname, String Course)  
  21.         {  
  22.             this.Id = Id;  
  23.             this.Name = Name;  
  24.             this.Surname = Surname;  
  25.             this.Course = Course;  
  26.         }  
  27.     }  
  28. } 

If the query string value is 1 then we are considering that the user wants to consume Version 1 of the Student Information service.

If the value is 2 then user wants to consume Version 2 of the Student Information service. Have a look at the following implementation.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Collections.Specialized;  
  4. using System.Linq;  
  5. using System.Web;  
  6. using System.Web.Http;  
  7. using System.Web.Mvc;  
  8. using TestWEB_API.Models;  
  9. namespace TestWEB_API.Controllers  
  10. {  
  11.     public class StudentController : ApiController  
  12.     {  
  13.         List<StudentInformation> SInfo = new List<StudentInformation>();  
  14.         StudentController()  
  15.         {  
  16.             SInfo.Add(new StudentInformation(1,"Sourav","Kayal","MCA"));  
  17.             SInfo.Add(new StudentInformation(2, "Ram""Kumar""B.Sc"));  
  18.             SInfo.Add(new StudentInformation(3, "Manish""Khanra""B.Tech"));  
  19.         }  
  20.         public IEnumerable<StudentInformation> Get(int ID)  
  21.         {  
  22.             NameValueCollection KV = HttpUtility.ParseQueryString(Request.RequestUri.Query);  
  23.             int a =Convert.ToInt16(KV[0]);  
  24.             if (KV.Keys.Count > 0)  
  25.             {  
  26.                 if (Convert.ToInt16(KV[0]) == 1)  
  27.                 {  
  28.                     //Consumer wants version 2  
  29.                     var stud = from m in SInfo.Where(s => s.Id == ID)  
  30.                                select new StudentInformation(  
  31.                                    m.Id,  
  32.                                    m.Name,  
  33.                                    m.Surname  
  34.                                );  
  35.                                return stud;  
  36.                 }  
  37.                 //They want Veesion  
  38.                 if (Convert.ToInt16(KV[0]) == 2)  
  39.                 {  
  40.                     //Consumer wants version 2  
  41.                     var stud = from m in SInfo.Where(s => s.Id == ID)  
  42.                                select new StudentInformation(  
  43.                                    m.Id,  
  44.                                    m.Name,  
  45.                                    m.Surname,  
  46.                                    m.Course  
  47.                                );  
  48.                     return stud;  
  49.                 }  
  50.             }  
  51.             else  
  52.             {  
  53.                 //They want V1 Which is by Default  
  54.                 var stud = from m in SInfo.Where(s => s.Id == ID)  
  55.                            select new StudentInformation(  
  56.                                m.Id,  
  57.                                m.Name,  
  58.                                m.Surname  
  59.                            );  
  60.                 return stud;  
  61.             }  
  62.             return null;  
  63.         }  
  64.     }  
  65. } 

The following is the output from calling Version 1 of the Student Information service and we are seeing that we are not getting course information in Version 1.

version 1 of student information service
 
Now, if we want to call Version 2 of the same service then we need to pass 2 in place of 1. And in this trial we are expecting data from Version 2 of the same service.

version 2 of student information service
Conclusion

In our previous article, we learned to implement versioning by adding a different controller. The drawback there was that once we introduce the new version we need to add another controller associated with that version.

The limitation of this mechanism is, we need to add one more if else pair when we want to introduce a new version of the same service.


Similar Articles