filmov
tv
Web API versioning using a custom header
Показать описание
Text version of the video
Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.
Slides
All ASP .NET Text Articles
All ASP .NET Slides
All Dot Net and SQL Server Tutorials in English
All Dot Net and SQL Server Tutorials in Arabic
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(controllerName, out controllerDescriptor))
{
return controllerDescriptor;
}
return null;
}
}
}
At this point, build the solution and issue a request to /api/students using Fiddler. Notice we have not specified our custom version header in the request. We get back StudentV1 objects, this is because we have set version 1 as the default in our code.
Let's issue another request using by setting custom version header to 2
Response from the service. Since we have specified the version as 2, we got StudentV2 objects back.
Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.
Slides
All ASP .NET Text Articles
All ASP .NET Slides
All Dot Net and SQL Server Tutorials in English
All Dot Net and SQL Server Tutorials in Arabic
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(controllerName, out controllerDescriptor))
{
return controllerDescriptor;
}
return null;
}
}
}
At this point, build the solution and issue a request to /api/students using Fiddler. Notice we have not specified our custom version header in the request. We get back StudentV1 objects, this is because we have set version 1 as the default in our code.
Let's issue another request using by setting custom version header to 2
Response from the service. Since we have specified the version as 2, we got StudentV2 objects back.
Комментарии