
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- Related Questions & Answers
- How to do versioning with accept header in C# ASP.NET WebAPI?
- How to do versioning with the Querystring parameter in C# ASP.NET WebAPI?
- How to do versioning with custom media type in C# ASP.NET WebAPI?
- How do we specify MIME type in Asp.Net WebAPI C#?
- How can we test C# Asp.Net WebAPI?
- How to resolve CORS issue in C# ASP.NET WebAPI?
- How to configure C# ASP.NET WebAPI in web.configure file?
- What is parameter binding in C# ASP.NET WebAPI?
- What is Content Negotiation in Asp.Net webAPI C#?
- How to consume Asp.Net WebAPI endpoints from other applications using C#?
- How to add custom message handlers to the pipeline in Asp.Net webAPI C#?
- What are the advantages of using C# ASP.NET WebAPI?
- What are built-in message handlers in Asp.Net webAPI C#?
- What is the usage of DelegatingHandler in Asp.Net webAPI C#?
- How to return custom result type from an action method in C# ASP.NET WebAPI?