Set first Route on Controller's Action method | ASP.NET Core 5.0 Web API Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
now since the mapping of controllers is done with the application now we have to define some routes on the controller inside this controllers folder we already have one controller the name is test controller just to understand the concept of routing let's add one more controller in this application let's right click on this folder go to add and click on this controller here you have to go inside this installed then command and click on this api you might also have the mvc but you have to focus on api and here we have three templates api controller empty api controller with redirect actions api controller with action using entity framework these templates are basically used for this scaffolding concept if you are using the first one you will get an empty controller for the second one you will get one more controller with some read write actions for the third one if you are using the entity framework then you can define your db context class and based on the db context class you will get the working action methods for the controller now since we are learning everything from scratch so we have to focus on the empty one let's choose this empty controller and click on the add button here we have to give the name of our controller and here you can notice that by default the controller is available as a suffix in the name either you can upgrade this name or you can keep it as it is let's leave it as it is and click on the add button so here you can see we got a values controller in this application the control is inherited directly from this controller base here we are using the api controller attribute and we are also using this route so let's remove this route for a while this controller let's add two methods don't focus on the return type of this method and what value we are returning we need to concept of the routing and routing is basically mapping the incoming request to this particular action method to work with the attribute routing in asp.net core web application we need to use one attribute and the name is route now let's define our first route for this particular action method if you are talking about the attributes it means you need to use these attributes above the method or the class to use this route attribute we need to use the square bracket and inside this square bracket we need to write route now let's see what we got in this route attribute right click on this one and choose go to definition here you can see this route attribute is basically a class it is inherited from attribute and i route template provider and here is the constructor for this route attribute class and inside this constructor we need to define the template and a template is nothing this is just the url that you are expecting from your browser so let's say i'm expecting get all something like this this is how we can define a route in asp.net code let's define one more similar route for this get all authors also let's copy this route paste it over here and here we can write like this save the changes and now let's run this application in debug mode the first url is get all we can copy this url from here let's append this url after this domain click on the enter button here you can notice that we got the output from that particular action method if i put a breakpoint over here and again refresh this request you can notice that we got the debugger over here click on the continue button now let's test the second route and here we go to the output from our second action method let's put a breakpoint again refresh this page here we got the debugger click on the continue button this is how you can define the very simple loud for your action method now let's say i want to append api in both the routes so i can simply write api over here and then use the forward slash api and the force less it is your choice if you want to append it you can if you do not want to append it then there is no it but basically i am just telling you the template that you can use multiple values also now let's run this application by pressing ctrl f5 if you will try to access this resource by using the get all then you will get an 404 because at this time this gethold does not exist we have to use the api over here press the enter button and this time you can notice that we got the output this is how you can define the simplest route for your accent method
Info
Channel: WebGentle
Views: 22,965
Rating: undefined out of 5
Keywords: asp.net core web api, web api asp.net core 5, web api asp.net core, .net 5 web api, api in .net core 5.0, asp.net core web api tutorial, asp.net web api core 5, Restful web api asp.net core, how to build restful apis with asp.net core 5, how to build restful apis with asp.net core, asp.net core, asp.net core web api tutorial for beginners, restful web api tutorial asp.net core, asp.net core web api tutorial webgentle, asp.net core web api tutorial nitish
Id: 8a5d73NzWrM
Channel Id: undefined
Length: 4min 50sec (290 seconds)
Published: Fri Apr 09 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.