Web API versioning using a custom header HD
Text version of the video http://csharp-video-tutorials.blogspot.com/2017/03/web-api-versioning-using-custom-header.html Slides http://csharp-video-tutorials.blogspot.com/2017/03/web-api-versioning-using-custom-header_1.html All ASP .NET Text Articles http://csharp-video-tutorials.blogspot.com/p/free-aspnet-video-tutorial.html All ASP .NET Slides http://csharp-video-tutorials.blogspot.com/p/aspnet-slides.html All Dot Net and SQL Server Tutorials in English https://www.youtube.com/user/kudvenkat/playlists?view=1&sort=dd All Dot Net and SQL Server Tutorials in Arabic https://www.youtube.com/c/KudvenkatArabic/playlists In this video we will discuss versioning a Web API Service using a custom version header. In our previous video, we have implemented a CustomControllerSelector. At the moment, this CustomControllerSelector is retrieving the version number for a query string parameter. To implement versioning using a custom version header, all we have to do is change the logic slightly int the CustomControllerSelector class to read the version number from the custom version header instead of from a query string parameter The changes that are required are commented, so it is self-explanatory. 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(); // Default the version number to 1 string versionNumber = "1"; // Comment the code that gets the version number from Query String // var versionQueryString = HttpUtility.ParseQueryString(request.RequestUri.Query); // if (versionQueryString["v"] != null) // { // versionNumber = versionQueryString["v"]; // } // Get the version number from Custom version header // This custom header can have any name. We have to use this // same header to specify the version when issuing a request string customHeader = "X-StudentService-Version"; if (request.Headers.Contains(customHeader)) { versionNumber = request.Headers.GetValues(customHeader).FirstOrDefault(); } HttpControllerDescriptor controllerDescriptor; if (versionNumber == "1") { controllerName = string.Concat(controllerName, "V1"); } else { controllerName = string.Concat(controllerName, "V2"); } if (controllers.TryGetValue(contr