Part 9 Routing in Web/REST api #1 || Asp.Net Core Web API Tutorials C#

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to web API tutorials I am venkat this is part 9 routing in web API how we can map the URI with the actual action inside a service if you want to look at the mapping or binding part of this one if we consider there is a controller inside the controllers there will be actions or action methods written every action method inside a web API controller will be attributed with verb and Route Suppose there is a action method in a controller that is get all students so that's the action that action is basically HTTP get action so to execute this one we need to call this URI from browser so this one is basically a localhost is a server which is running under and API student is a URI okay this is the full URL if you run this URL in a browser with HTTP get verb then only this method will get triggered only this part will not help us so if you want to trigger this particular action get all students action so we need to combine these two and call it okay if you look at the postman where you can test the web API service there you need to select the kind of request you want to do it is a HTTP gate or post or delete or put something like that then the URI so it is the combination of the verb and URI so let's look into the next one next action is get student by ID this is the second action I have in my controller so this action is also HTTP get how come a single controller cannot have a multiple HTTP gets right we can how you can execute a method here is with the help of which state of a verb and the route so the combination of HTTP verb and the route should be unique okay if you look at the route it is a API slash student slash one so this is different compared to the previous route previous route is API slash student so when you hit API slash student from browser as a HTTP get call you will get all the students but if you trigger the below URI API slash student slash 1 with HTTP get you will get single student okay which who is matching that ID here you can notice that both are HTTP gets but there is a change in URI here also the combination of the verb and Route will trigger this get student by ID so next one is a delete student by ID so the second one here is a get student by ID that will fetch the student based on the ID and it gives back the a particular student details but coming to this delete student by ID here it will find the student based on that ID and it will delete that ID so the verb for this one is a HTTP delete and here if you notice the URI URI is similar to the get student by ID URL so here also it is a API slash student slash one the above URI also same API slash student slash one but how are you able to delete the student how are you able to get the student so it is deciding based on the HTTP verb you are using this one HTTP verb so here you need to notice one thing that is when you are writing a action method or consuming an action method you need to be careful about the HTTP verb you are using so if you notice these two are same if you hit the same URL from the postman or browser if you make the request as a HTTP get then you will get a student related details by mistake if you do the same thing with the HTTP delete that particular student will be deleted because this URI is same so here if you want to understand the routing of web API if you want to execute a action you need to use routing concept and coming to the routing concept routing is basically combination of HTTP verb and the routes go into the code if you notice this one here it is a API student and if you notice these two API student 1 API student 1. these two Uris are same but first one is triggering A2 second one is triggering A3 so that is deciding based on HTTP action HTTP get is calling a to http delete is calling the A3 and here certificate and here also HTTP get so where you need to be careful when you are preparing the routes okay let's look the same thing inside the code here we need to take four actions so here I have one action already let me build the other three so this one is a get students the second one will be get student by ID so the next one will be get student by name the next one will be delete student okay for delete student we need integer ID and for fetching also we need integer ID and fetch by name needs string name okay right we have the student by ID dot where and tends to n dot ID equal to ID DOT first or default that will fetch so here it is a single record returning it is of type student and here also single record returning and here also the same thing but here instead of the ID we need to compare it with the name foreign Dot where M tends to n dot ID equal to ID so here we can do one thing where student call to that first or default College repository dot students dot remove of student let's go back to the student and we have removed it then return true so here it is a Boolean type okay done so now we have written all four methods and we have added all HTTP verbs so all these three arcades and this one is a HTTP delete and come into this one when we are working with the API controller attribute attribute based routing is mandatory so let's test that I have commented it out and now I am running it so it will throw us an error saying when you are using API controller attribute based routing is mandatory see that so action methods on controllers annotated with API controller attribute annotated with API controller attribute must be attribute routed so it is saying wherever we are using this API controller we must go for attribute routing and coming to the this route if you want to put a attribute based routing if you want to use it you need to use the route attribute and inside the route attribute you can mention two things first one is a template and second one is the route name okay this is the template and if you want the dynamic name here so Dynamic controller name suppose you can also work like this so if you put like this the URI will be like this API slash student but if you modify the student name student courses so if you want your endpoint to modify it based on the controller name it will not be modified in this case so if you want this controller name add it to your route you should use like this if you put a square brace if you put a square brush like this inside controller it will get replaced with the controller name if you modify this controller in future the modified value will go here if you don't want the controller name reflecting here you can go for the fixed one like this okay so for now I'll go like this that's the one and coming to the actions inside the controller so all these actions will get this route as the prefixed route and on top of that route if you want to add you can add the route so you can add the route in two different ways using the route attribute so here I don't want to call get all as a API student I want to get it as a student slash all this is Meaningful API slash student slash all is a meaningful I feel so I'll make it as a all so this is basically API slash student where is this student coming from this one so I want only the student I'll remove this one just now I have modified so now it is the API slash student slash all so the action route will be added to this for this one I'll make it same thing but I'll give the different value so this is the ID it is a dynamic value so whatever we are putting inside the flower brace it is a parameter or the dynamic value if you notice this one in the place of this one two three student IDs we can pass anything that will be captured by this ID parameter and passed on to this one that's the ID so we can also mention the name of this particular route so that's the template and I want to mention a name so get all students this is just a route this will not be used anywhere this is the name of this particular route here I want to give the name as get student by ID and the same route I can also use here but here it is a get student by name so but if you notice there are some places you can see this template and name will be added to the HTTP verb we can do that one also so if you want to use the routing and HTTP attribute in a single attribute we can do that so that is like oh I can add the template to this one and name is like that so this is also fine so what I mean to say here is we can Define the routes into different ways that is using the route attribute and also using the HTTP verb attribute okay so we can Define the routes in these two different ways and here it is student slash ID so here we are getting ID so here I can use the same thing for my routing and here it is a ID I'm using the same HTTP attribute for routing also so here I can the name is optional actually so here name equal to delete student by ID that's the name I have given this is just a name for that particular route okay so route defined now so but here it is different get student by name so it is a string value so I should use the name here and if you notice these two methods if you notice these two methods so you can see one issue so here it is a dynamic value here also it is a dynamic value which one will get triggered that's a question right so let's see that in action so if you want to define a route based attribute you need to use the attribute of route and in that route if you notice there are two things first thing is a template then if you want to Define name also you can Define like this so template and the name so route template route name you can see like that so this is the route template and Route name we can Define the routes in two different ways using first one is using route at a bit and second one is using HTTP battery so let me run this one if you see this one we can clearly see that so so here it is uh the base path is a API slash student if you notice here all these are having API slash student okay and this route template is given all and all here and this one is the id id here so here delete is ID delete by id id here
Info
Channel: Nehanth World
Views: 14,190
Rating: undefined out of 5
Keywords: Part 9 Routing in web api #1 || Asp.Net Core Web API Tutorials, web api tutorials, asp.net core, c#
Id: 1CwZB1cQXXc
Channel Id: undefined
Length: 14min 52sec (892 seconds)
Published: Wed Nov 16 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.