Web API versioning using querystring parameter

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 Web API Text Articles and Slides

All ASP .NET Web API Videos

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 Service using a QueryString parameter.

Before we implement versioning using a querystring parameter. First let's understand how a controller is selected when a request is issued to a web api service. For example, let us understand how a controller is selected when a rquest is issued to the following URI
/api/students/1

In Web API, there is a class called DefaultHttpControllerSelector. This class has a method called SelectController() that selects the controller based on the information it has in the URI.

In the URI we have
1. The name of the controller, in this case students
2. The id parameter value, in this case 1

So from the URI, the SelectController() method takes the name of the controller in this case "Students" and finds "StudentsController" and returns it. This is the default implementation that we get out of the box.

This default implementation will not work for us because, in our service we do not have controller that is named StudentsController. Instead we have
1. StudentsV1Controller and
2. StudentsV2Controller

When a request is issued to the following URI, depending on the query string parameter "v" value we want to select the controller. If the value is 1, select StudentsV1Controller, and if it is 2, then select StudentsV2Controller.
/api/students?v=1

Step 1 : Since the default controller selector implementation provided by Web API does not work for us, we have to provide our own custom controller selector implementation. To do this
1. Add a folder to the web api project. Name it "Custom"
2. Add a class file to the folder. Name it "CustomControllerSelector".

For the "CustomControllerSelector" class code please check the link below

config.Services.Replace(typeof(IHttpControllerSelector),
new CustomControllerSelector(config));

config.Routes.MapHttpRoute(
name: "DefaultRoute",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);

Step 4 : Remove [Route] attribute, from action methods in StudentsV1Controller and StudentsV2Controller
Рекомендации по теме
Комментарии
Автор

As usual Thanks a lot Mr.Venkat. So which one is best method for versioning API : URI or querystring parameter for enterprise application?

ramchandrathakkar
Автор

Hi sir, Which is the better way to implement versioning ?

gauthammeka
Автор

I have gone through many video tutorials to understand dependency injection and unity configuration can you please make a video on that too. I am sure that will clear my doubts as I am habituated to Kudvenkat's Quality Teaching so I am not able to digest other thing. Please do it when possible thanks in advance

saurabhchauhan
Автор

Can you please make a tutorial of versioning on application that use entity framework and odata ?

gauthammeka
Автор

Hello sir,
I have one doubt here, you have commented Route attribute for an action method,
Then how it will work for multiple get action method in a single controller? would you please provide sample code for this scenario.

sagarbiradar
Автор

Q. What is dependency injection in mvc 5 with example ?
plz upload a video

sandippunalkar
Автор

Mr. Venkat, I pray to God to explain your heart and guide you to Islam just as God explained your heart to give people knowledge for free.
I am engineer Mahmoud from Egypt

ypwygk
join shbcf.ru