Attribute routing in ASP NET Web API 2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this is part three of a speed ordinance Web API tutorial in this video we'll discuss attribute routing introduced in asp.net Web API - let's understand attribute outing with an example let's flip to visual studio what I have done so far is created a new web API project I named it Web API the first thing that we are going to do is add a student class so let's right click on the models folder add a class file let's name or class file student the student class is going to have two properties the first property type is integer and it's going to be ID the second property is name and its type is going to be string so that's our student class the next thing that we are going to do is add students controller to our controllers folder so let's right click on the controllers folder add and we want to add a controller and the type of controller that we are going to add is Web API to empty controller let's name or controller students controller and within the students controller the first thing that we are going to do is create a static list of students so let's create a static variable of type list of student remember the student class is present in a different namespace Web API dot models so let's bring that namespace in let's call the variable students now let's build a list here let's create a new student object and remember the student object has got two properties ID and name let's create a couple of more students in the interest of time I have already typed the required code so let's copy and paste it right here so at the moment we've got three students now let's include a public method here which is going to return I enumerable of student objects let's call this method get' and all it's going to do is return whatever students list that we have within the static variable students let's include another get method now this method is going to return as a single student object so this method will get student by ID so from the students list give me the first a default student where student ID matches with the ID that we are passing into this get method let's have one more public method here and this method is going to return I enumerable of string objects and let's call this method get student courses now each student can be enrolled into a list of courses and we want this method to return the list of courses by student ID so it's going to have a parameter ID and what we want to do is hard code that list here so if ID equals 1 so whatever ID we are passing if that is 1 then we want to return a new list of string objects and let's build our list of courses here so let's say student with ID 1 is enrolled in c-sharp asp.net and secret server and let's do the same thing for student ID 2 & 3 in the interest of time I have already typed the code so let's copy and paste that right here so student ID 2 is enrolled in these courses and student ID 3 is enrolled in these courses now if we look at the students and students courses we have hard-coded them within our application in a real world application we would be getting the students and student courses from a database table to keep this example simple i have hard-coded them here so at the moment we've got three methods within our students controller this ket method is going to return us the list of all students this method is going to return as a student by ID and this method is going to return as student courses by ID let's run our application by pressing ctrl f5 and navigate to / API slash students notice we get the list of students as expected so this URL right here / API slash students it's mapped to this controller action method within our students controller so this mapping is done by the default route that we already have in place so we have the default route within our Web API config dot CSV file we have that default route here this routing mechanism is called as convention based routing and this route is created by the Web API project template for us automatically when we created this new project so with this convention based routing and that the students controller in place this URL right here / API slash students is mapped to the scape controller action method now let's also pass student ID in the URI so we have now / API slash student slash 1 so we extract this URI to be mapped to this action method which is taking student ID and then return us that specific student but look at what happens when we issue this request we get an error and look at what the error says it says multiple actions were found that match the request so basically it's saying between students controller has got the sket method and we also have got get student courses within that same students controller and to this URI does not know between these two methods which method to use so both the methods start the divert get and both of them have got the ID parameter so the framework is confused for this URI which action method to use now we can very easily resolve this by using attribute routing and here is what we want to do we want this URI slash API slash student slash one this one to be mapped to this controller action method and then written as that specific student but on the other hand along with the student ID if I also specify courses in the URI like this then we want all courses to which that student ID one has subscribed to okay so this should be mapped to this controller action method and then obviously it's going to return us the student courses into which that specific student has enrolled enter so to achieve this all we have to do is decorate this method with the route attribute so let's use the route attribute and we want this action method to be mapped to API slash students slash and we are going to pass an ID parameter and then followed by that we are going to have this little string courses so if we pass one as the ID then we want that specific student courses okay so let's give our solution a build and issue a request to this URI slash API slash students / 1 / courses notice we get student 1 courses as expected so this URI is mapped to this controller action method get student courses notice this method is decorated with route attribute so that URI is mapped action method using attribute routing and now if you issue a Request 2/1 without that little string courses notice we get student 1 details so this URI slash API slash student slash 1 this is map to this controller action method and notice on this method they're not using the road attribute but still the mapping is working because behind the scenes it's using convention based routing so what is attribute loading using the route attribute to define routes is called attribute routing what are the advantages of using attribute routing attribute routing gives us more control over the you our eyes then convention based routing creating your eye patterns like hierarchies of resources for example students have courses departments have employees creating patterns like this is very difficult with convention based routing whereas with attribute routing all you have to do is use the route attribute as shown right here so here we're creating a pattern students have courses it start easy whereas with convention based routing it's a bit difficult and does not scale very well so how to enable attribute routing in web api to attribute routing is enabled by default so this line right here config dot map HTTP attribute routes this enables attribute routing so if you look at our baby pea convict dot CSV file notice we have that line right here which enables attribute routing if you remove this line or comment this line then attribute routing is not going to work it can be used both attribute routing and convention based routing in a single web API project yes both of them can be used in a single web API project the controller action methods that have this alt attribute use this attribute routing and the others without the route attribute uses convention based routing thank you for listening and have a great day
Info
Channel: kudvenkat
Views: 124,951
Rating: undefined out of 5
Keywords: web api 2 attribute routing example, route attribute web api 2, asp.net web api hierarchical routes, attribute routing vs conventional routing, web api enable attribute routing, multiple actions were found that match the request web api
Id: jvnQavMzfe4
Channel Id: undefined
Length: 10min 7sec (607 seconds)
Published: Tue Feb 14 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.