How to do Web API versioning with URI in C# ASP.NET WebAPI?


Once a Web API service is made public, different client applications start using our Web API services. As the business grows and requirements change, we may have to change the services as well, but the changes to the services should be done in way that does not break any existing client applications.

This is when Web API versioning helps. We keep the existing services as is, so we are not breaking the existing client applications, and develop a new version of the service that new client applications can start using.

One of the option to implement versioning is by using URI. Below is an example on how to implement the same.

Example

Let us consider a version 1 (V1) of sudent controller which has the following action methods.

Student Model V1

namespace DemoWebApplication.Models{
   public class StudentV1{
      public int Id { get; set; }
      public string Name { get; set; }
   }
}

Student Controller V1

using DemoWebApplication.Models;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace DemoWebApplication.Controllers{
   public class StudentV1Controller : ApiController{
      List<StudentV1> students = new List<StudentV1>{
         new StudentV1{
            Id = 1,
            Name = "Mark"
         },
         new StudentV1{
            Id = 2,
            Name = "John"
         }
      };
      [Route("api/v1/students")]
      public IEnumerable<StudentV1> Get(){
         return students;
      }
      [Route("api/v1/students/{id}")]
      public StudentV1 Get(int id){
         var studentForId = students.FirstOrDefault(x => x.Id == id);
         return studentForId;
      }
   }
}

In the above example we have used Attribute Routing to implement the versioning. The output of the above example is shown below −

Now let us say in the student controller, business has proposed a new change only for the new users and the existing users should still use the Version 1. So in this case we have to introduce Version 2 (V2).

Student Model V2

Example

namespace DemoWebApplication.Models{
   public class StudentV2{
      public int Id { get; set; }
      public string FirstName { get; set; }
      public string LastName { get; set; }
   }
}

Student Controller V2

using DemoWebApplication.Models;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace DemoWebApplication.Controllers{
   public class StudentV2Controller : ApiController{
      List<StudentV2> students = new List<StudentV2>{
         new StudentV2{
            Id = 1,
            FirstName = "Roger",
            LastName = "Federer"
         },
         new StudentV2{
            Id = 2,
            FirstName = "Tom",
            LastName = "Bruce"
         }
      };
      [Route("api/v2/students")]
      public IEnumerable<StudentV2> Get(){
         return students;
      }
      [Route("api/v2/students/{id}")]
      public StudentV2 Get(int id){
         var studentForId = students.FirstOrDefault(x => x.Id == id);
         return studentForId;
      }
   }
}

Output

The output of the above example is shown below.


Updated on: 19-Aug-2020

558 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements