
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Versioning with QueryString Parameter in C# ASP.NET WebAPI
The DefaultHttpControllerSelector class in web api is responsible for selecting the appropriate controller action method that we send in the URI.
Say we have to implement versioning in the query string like below
v=1 StudentsV1Controller (Version 1) v=2 StudentsV2Controller (Version 2)
If we pass the versioning information in the query string like http://localhost:58174/api/student?v=1 will result in 404 Not Found error response because the SelectController() method which is present in DefaultHttpControllerSelector will look for the StudentsController but we have only StudentsV1Controller and StudentsV2Controller.
To handle this case, we should add our own CustomControllerSelector which implements the DefaultHttpControllerSelector class.
CustomControllerSelector −
Example
using System.Net.Http; using System.Web; using System.Web.Http; using System.Web.Http.Controllers; using System.Web.Http.Dispatcher; namespace WebAPI.Custom{ public class CustomControllerSelector : DefaultHttpControllerSelector{ private HttpConfiguration _config; public CustomControllerSelector(HttpConfiguration config) : base(config){ _config = config; } public override HttpControllerDescriptor SelectController(HttpRequestMessage request){ var controllers = GetControllerMapping(); var routeData = request.GetRouteData(); var controllerName = routeData.Values["controller"].ToString(); string versionNumber = "1"; var versionQueryString = HttpUtility.ParseQueryString(request.RequestUri.Query); if (versionQueryString["v"] != null){ versionNumber = versionQueryString["v"]; } if (versionNumber == "1"){ controllerName = controllerName + "V1"; } else if (versionNumber == "2"){ controllerName = controllerName + "V2"; } HttpControllerDescriptor controllerDescriptor; if (controllers.TryGetValue(controllerName, out controllerDescriptor)){ return controllerDescriptor; } return null; } } }
The next thing that we need to replace the default controller selector with our custom controller selector. This is done in WebApiConfig.cs file. Notice we are replacing IHttpControllerSelector, with our CustomControllerSelector. DefaultHttpControllerSelector implements IHttpControllerSelector, so that is the reason we are replacing IHttpControllerSelector.
Example
public static class WebApiConfig{ public static void Register(HttpConfiguration config){ config.Services.Replace(typeof(IHttpControllerSelector), new CustomControllerSelector(config)); config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } }
StudenV1Controller −
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" } }; public IEnumerable<StudentV1> Get(){ return students; } public StudentV1 Get(int id){ var studentForId = students.FirstOrDefault(x => x.Id == id); return studentForId; } } }
StudentV2Controller −
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" } }; public IEnumerable<StudentV2> Get(){ return students; } public StudentV2 Get(int id){ var studentForId = students.FirstOrDefault(x => x.Id == id); return studentForId; } } }
Output
Below outputs shows the results that we get from StudentV1 and StudentV2 controllers with versioning in query string.