Web API versioning using a custom header

preview_player
Показать описание
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.
Рекомендации по теме
Комментарии
Автор

Thank you Master Venkat for your in-depth guidance.

jeewanintube
Автор

IMO, it would be a better option to use split() method on versionNumber @6:10

maheshkudikala
Автор

Hi Venkat gaaru. I am very much impressed the way of your presentation and explanation . Almost I have gone through your all tutorials . I am requesting you to explain WebAPI token based authentication with our own/custom database . I have searched a lot . I could not find So please help me Sir . And if possible Can u make a tutorial on Angular 2 or Typescript

krishnasarigopula
Автор

5:45 Why we don't use Split-method instead of Substring one?

MsAlexandr
Автор

Hi Venkat.. if u know anout Dynamics crm programming.. please post those videos also

bhargavbennalli
Автор

Thanks! This I've needed to pass token data.

fhr
Автор

kudvenkat another great video.Please upload using multilanguage functionality with ASP.NET MVC 5, Please upload web api vs angularjs too

suleymanov
Автор

Thanks for all your effort. I need to know how to use API to show video steaming from camera. I hope you can help me

ahmedbadawy
Автор

<b>Hi Venkat gaaru. I am very much impressed the way of your presentation and explanation . Almost I have gone through your all tutorials . I am requesting you to explain WebAPI token based authentication with our own/custom database . I have searched a lot . I could not find So please help me Sir . And if possible Can u make a tutorial on Angular 2 or Typescript </b>

krishnasarigopula
Автор

How this custom header will be specified when we are making request from a browser, means in fiddler we are adding the header part. How can we achieve from browser

sureshs
Автор

Hi! Can you make a video about how to make ASP.NET chart controls width responsive?

hrvojekrolo
Автор

sir, how to create load more button system and display image from database

gamezblock
Автор

Hi sir.. can you please upload the videos based on webpack+typescript+angular2

MapsTutionpoint
Автор

typescript with visual stuidio and angular2

peterl
Автор

Hi Venkat gaaru. I am very much impressed the way of your presentation and explanation . Almost I have gone through your all tutorials . I am requesting you to explain WebAPI token based authentication with our own/custom database . I have searched a lot . I could not find So please help me Sir . And if possible Can u make a tutorial on Angular 2 or Typescript

krishnasarigopula
Автор

<b>Hi Venkat gaaru. I am very much impressed the way of your presentation and explanation . Almost I have gone through your all tutorials . I am requesting you to explain WebAPI token based authentication with our own/custom database . I have searched a lot . I could not find So please help me Sir . And if possible Can u make a tutorial on Angular 2 or Typescript </b>

krishnasarigopula