How to Implement API VERSIONING IN ASP.NET CORE Web API -3 Different Ways- API Versioning Swagger

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to macro code if you're new to this channel consider subscribing so today video we are going to learn about API versioning in sp. net core and actually understand some of the best practices so in today's video we are going to learn uh and and actually understand what is API versioning and why do do we need it different ways to implement API versioning in. netc install some of the nuggets and some of the Nuggets that we need to achieve this and you're going to understand three types of uh three ways of implementation one is the URL based API versioning query based API versioning and HTTP header based API versioning so what is API versioning some of you might be wondering or asking why what is API versioning so before deploying an API there's actually a checklist of a few items that you are uh that are very vital for you to consider so API versioning actually is one of the top priority when dealing with apis so it is highly crucial to anticipate changes uh that may require that may be required once the API is actually published and a few clients are already are using the API so you can imagine updating API that is being used by some of your clients and you don't want to have them changing every end point for the API that we had deployed for them so and that is the reason as to why API versioning comes in so API versioning in a speed net is a technique by which different clients can get different implementation of the same controller based on the request or the URL so essentially you build an API that has multiple versions that may behave differently so a great approach is to use API versioning to actually uh encounter this and also uh don't compromise with integrity and the availability of the data for the already existing client application so without uh Much Ado let's just begin and discuss the different ways to implement the API versioning in sp. netc so there are multiple ways to achieve API versioning in a speedet application so the used approaches are the query string based and we have the URL based and the HTTP header based so these uh these are the most common ones and they are the ones that I'm going to discuss today so if you have others like the accept Eder or the media type comment down below and let me know which uh is the best that you use so to get started let's just create our API so I'm going to create an API sp. net called Web API then I'm going to name my API students API students management API so I'm going to name students management API then create next so I'm I'm using Net 7 then uh enable configure for https then use controllers so we are going to use controllers on our next video we can do a video about minimal apis then we are going also to enable open API support So once we create this our API will be created so you can see we have the weather forecast uh sample class here we also have the program CS file then under controllers we have the weather forecast controllers if we launch these you can see this is our API a students management API then if we try it out we are able to get the data so you can see this is the data so this is uh this is what we need so to begin with now we are going to install some nuggets so right click on the right click on our application then manage nuggets packages then we going to install one of the Nugget called API versioning MVC so that is on the browse section so I want us to install two things here I want us to install sp. versioning MVC so apply then I also want us to install sp. net sp. versioning MVC API Explorer so after we have done that then the next thing that we going to do is to configure our API versioning so come to our program. CS under API and our program. CS file I'm going to add some code here so this is the code that I've added so we are adding a API versioning so for default version we are going to use a version one then assume default version when unspecified then if version is not specified then we can take the default version then we report versions uh then you can also uh have the AP I version reader so we'll be updating this as we go along so this is what we'll be updating as we we go along so the next thing that we need to update is on our controller so I'm going to do away with our weather Focus controller then I'm going to create for us to have a clean uh API versions based on uh how we are going to version our API so let's create another folder call V1 so I'm going to create here a an empty controller for students so I'm going to K this as students so this is our controller so for this controller I'm going to have a public string then get so it will be a it should uh return a string so we can say return then I'll say students students data from so you can call this API version you can call Api V1 so what we need to do here on our get so you can say this is HTTP get so on our API we can now do API version then you can specify this as version one then another thing we can also specify another folder we can say V1 V2 now this is our version two then we can copy this paste it here then we can specify this as version two and remember this is now on V2 then uh this is our controller this is the same then we can also specify version three so assuming you have you have been updating your API so instead of changing the same same controllers you can actually specify different controllers and actually make your update and provide it as the latest uh version so it is the same controllers but now you are now changing the version with some new updates so on our controller so I want us to change something so on our API we need to specify the version so we can say V then we do that then we say version then API version then we do that then we do that so this is how we specify the version then I want us to map it so I want us to map our end points so I'll say m map to version so it will it will map this get to version one so I'm going to copy this so I'm going to copy this to version two then I'll change this to two and this to two two as well so these will be from version two so this is data getting data from version two then this is the same same controller same end points but different versions so we come to version three we now copying this as well so this will be version three then this will be version three as well then this will be version three so if I try to launch our API we'll have some issues with the swag so our next point now is to to configure our Swagger you can see we have an issue with swager so what I'm going to do I want us to define a folder so I want us to define a folder on our project and call it uh so right click on our project add a folder called swager so on I want us to now kind of uh design our swager so you can you can now see my code so this is the one of the classes and swager folder I've added this configure swager options then configure swager options have uh so this is the code that I've written so we are going to specify the name of our code we can call our API called students management management web API description so you can say this is the description of students management web API then you can be able to specify the other say this is macro code at gmail.com and say the name is mcro code so these are some of the S then if it's duplicated then you specify that this API version has been duplicated so this is some of the things that is on this class that I've added guys so I'm just adding this so that you can be able to proceed faster so as you can see you can be able to copy this so you can borrow this so and swaga so you'll be having another class so and swaga default values so this is the code that I'm adding then I'm importing that so this is my code and swaga so I'm having two classes and swaga folder the configure swager options that you have gone through and the Swagger default values so this is the code that I've added so the next thing that you're going to do is we going to add this to to configure these on our program.cs so one we going to add add transient then configure Swagger options these ones that we had added and configure Swagger options so then so swag gen options and this one then we add the custom operation filter with the default values for our API versioning so I'm going to add those line of code so you can see Builder Services API versioning then uh the group format and the substitute uh version URL so then so on our swaga UI I'm going to update this now to reflect our end points with API versions so on aaga UI I'm going to get the description then for each of the description I I take the group name so this is what we need to update on our swaga UI so you can actually borrow this so after that after we are done with that our API should be up and running unless there's anything that we've missed out you can see so this is our API so you can see the students management web API description of the students so you can actually put in a description here you can contact micro code and you can see this is version one so if we try this and execute you can see students data from API version one so this is version one of our API so and you can see it is actually specifying the the version at the top here you can see there is versioning so you can see we have version two this is version two if I try and execute you can see students data from API version two then we have version three this is version three so if we try these and execute this is uh students data from version two so if we go and try extending these uh controllers so you can see how we have specified it we have it within the folders based on the based on the versions and are the controllers so you can actually rename these controllers and we can call so we can have your post can see we have that this is HTTP HTT post then you can have another one here you can say [Music] delete I can say [Music] at [Music] delete then you can also have another one can say put can say put then you can have this as HTT put so this is H so if I take this copy this then I can replicate it here then I'll just change the versions you can see it is using the same same same same end point same same controller I mean then I can go here can specify these and I can say this is three so instead of changing the same controller you can actually be doing this then you provide the latest uh API version so you can do as many versions as you can so you can see this is our API version one students and this is the default one so if we do that you can see version one version one put uh post everything is here so if we go to version two you can see this is changed and we can say version three assuming version one is duplicated and version two is duplicated and you want all your clients to be using version three so how to do that you come to version one here and within the API version so remember Guys these two lines are very important so if we come to our API version and we just say comma then you say duplicated it should be should be here duplicated yes so inside the API version then we can say duplicated we say it's true so that means this version has been duplicated and it is not being used once more can do that with version two so let's see what is happening so if we launch it we should be able to have you can see it has been flagged so our API version one has been duplicated API version two is also duplicated and you can see API version three is active so that is how you do API versioning and in case you have any comment comment Down Below in case you have any suggestion comment down below and we'll be posting the source code so if you need the source code we'll share the link within the description like the video comment down below share and remember to subscribe for those who haven't subscribed so these are some of the code that we have we have a controller for version one version two and version three then the swaga we have these two classes that you have just uh updated so that you able to see then we also have uh this one default values so this is some of the custom classes that we have done so under program CS we have updated our swaga UI which is still under development but you can get these out of uh but it is recommended to be there then we have these uh versioning and these uh code that we have updated so that is it guys so if you're new to this channel consider subscribing like our video and comment down below see you on our next video [Music] bye
Info
Channel: Macro Code
Views: 1,690
Rating: undefined out of 5
Keywords: AmigosCode, .NET, .NETCORE, Sundeep, Saradhi, freecodeCamp, ProgrammingWithMosh, MikeDane, IamTimCorey, TeddySmith, FireShip, DaniKrossing, Stefan Mischook, Coder Foundry, Jose Montemagno, .NET 7, .NET 6, Visual Studio, Nick, Nickchapsas, Jake Wright, Keep On Coding, QuestPond, Developer Filip, TechWorld With Nana, Techworld, GotoConferences, CRUD, BoostMyTool, WEBSERVER, .NET CORE, Tech with Tim, Patrick God, Milan Jovanovic, dotnet, IAmTimCorey, Microsoft Developer, Api, Swagger, Apiversioning, APICRUD
Id: 1twgnDryEg4
Channel Id: undefined
Length: 19min 27sec (1167 seconds)
Published: Tue Oct 17 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.