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.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace TestWEB_API.Models
{
[Serializable]
public class StudentInformation
{
public int Id;
public string Name;
public string Surname;
public string Course;
public StudentInformation(int Id, string Name, string Surname)
{
this.Id = Id;
this.Name = Name;
this.Surname = Surname;
}
public StudentInformation(int Id, string Name, string Surname, String Course)
{
this.Id = Id;
this.Name = Name;
this.Surname = Surname;
this.Course = Course;
}
}
}
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.
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using TestWEB_API.Models;
namespace TestWEB_API.Controllers
{
public class StudentController : ApiController
{
List<StudentInformation> SInfo = new List<StudentInformation>();
StudentController()
{
SInfo.Add(new StudentInformation(1,"Sourav","Kayal","MCA"));
SInfo.Add(new StudentInformation(2, "Ram", "Kumar", "B.Sc"));
SInfo.Add(new StudentInformation(3, "Manish", "Khanra", "B.Tech"));
}
public IEnumerable<StudentInformation> Get(int ID)
{
NameValueCollection KV = HttpUtility.ParseQueryString(Request.RequestUri.Query);
int a =Convert.ToInt16(KV[0]);
if (KV.Keys.Count > 0)
{
if (Convert.ToInt16(KV[0]) == 1)
{
//Consumer wants version 2
var stud = from m in SInfo.Where(s => s.Id == ID)
select new StudentInformation(
m.Id,
m.Name,
m.Surname
);
return stud;
}
//They want Veesion
if (Convert.ToInt16(KV[0]) == 2)
{
//Consumer wants version 2
var stud = from m in SInfo.Where(s => s.Id == ID)
select new StudentInformation(
m.Id,
m.Name,
m.Surname,
m.Course
);
return stud;
}
}
else
{
//They want V1 Which is by Default
var stud = from m in SInfo.Where(s => s.Id == ID)
select new StudentInformation(
m.Id,
m.Name,
m.Surname
);
return stud;
}
return null;
}
}
}
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.
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.
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.