Part 18. Routing in ASP NET Core MVC. | AspNetCore | .NET5

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello friends welcome to programming concepts my name is amit and this is part 18 of asp.net core mvc tutorial in this video we will talk about routing in asp.net core mvc and we will create them practically this is continuation to part 17 controllers in asp.net core mvc so please watch it before proceeding to this one i shared the link in the description routing in asp.net core mvc is a mechanism through which incoming requests are mapped to controllers and their actions so we have two types of routing in asp.net core mvc the first one is conventional and second one is attribute routing so let's talk about conventional routing first so conventional routing is used with controllers and views and this is the flow that we had discussed in part 13 of this video series all right so when user tries to access our application with any url we need to prepare our middleware to provide correct data files or pages so we need to write the logic in our middleware if the request came with a certain url like where we need to route it to and to implement this microsoft already provided very simple extension methods or we can say middleware so it is a three-step process to implement conventional routing and the step one is add use endpoints middleware and define the routes step 2 add use routing middleware before use endpoints middleware to select which route to execute and the last one is add required services which supports use endpoints and configure services method which means inject routing dependencies so there are many services that supports routing like ad controllers with views at mvc add mvc core etc eventually what all these services actually doing that we will discuss maybe in some other video for now just think of them as a methods provided by microsoft to make our routes work all right so let's go to visual studio and this is the project which we are working with in our last video we have created one action method which is just returning a simple string so let's implement conventional routing so that our application can process this request for that let's get back to startup.cs class and the first step is add use endpoint middleware so let's type app.use and points bracket open and if you go to its definition you can see it is present under endpoint routing application builders extension class and you can see in the parameter it is expecting i application builder and action of i endpoint route builder so this is an extension method an endpoint middleware will execute the endpoints associated with the current request let's get back to our middleware method and let's define a variable endpoints you can name it anything here we are dealing with endpoints so we are naming it in that way all right such that open the curly braces and close the method for better readability in advance if you write endpoints then you can see it is offering many methods to perform various tasks around routing let's use map controller routes close statement with a semicolon so the first parameter is name so you can give any meaningful name which you feel is suitable for your route as we are targeting the employee index action method which we will be using to display list of employees so let me name it employee list next will be pattern of your url let's say when my user navigate to list of employee like www.proconcepts.com list of employee then i want this route to be executed so my pattern will be list of employee next is which controller and action method it should go so when the request came where it should go because if you remember in a vc pattern it's the controller which will process your request so write defaults colon culibris is open so my controller will be employee so write controller is equal to employ and action will be equal to index and that's it our first route is ready let's run our application here you can see the error so let me read the error first so end point routing middleware matches endpoints set up by endpoint middleware and so must be added to the request execution pipeline before endpoint middleware please add routing middleware by calling i applicationbuilder.userrouting inside the call to configure in the application startup code so it's simply because we skipped this step 2 which is to apply use routing middleware before use endpoints so let's correct this error and let's run the program once again to get new exception can you guess what will be the exception so if your guess is we haven't implemented this step three to add required services within configure services method then you are absolutely correct let's see the exception message so unable to find the required services please add all the required services by calling i service collection dot add controllers inside the call to configure services in the application startup code so let's fix this error by adding services dot add controllers with views within configure services methods and now let's run the program now we will get the clean run let me navigate to slash list of employees you can see we are getting the required message which is pro controller okay so let's get back to visual studio and let me change this message to list of employees now obviously in real time any application will have more than one controller an action method so let's create another action method so just copy and paste and let's name it delete employee which is expecting id of type integer and let's change the written message and type controller is equal to employ and action is equal to delete employee now for this we need to create another route right so within our middleware let's copy and paste the existing route and let's name it delete employee and let's say the pattern will be delete employ and because we are expecting id as an input parameter let's add slash id with curly braces all right next is we need to target employee controller and delete employee action so let's change the action method now our second conventional route is also ready it will target the delete employee action method so let's run the program let's navigate to delete employee slash one you can see the proper message is printed and what if i don't pass id then you can see it will throw page not found exception oh so we have a terminal middleware at the end that's why our middleware gets something to process this request let's delete that middleware and run the program once again let's navigate to delete employee and now you can see the page not found exception so to fix this we only need to append question mark to make it optional so let's append after id and let's run the program once again and now if you navigate to delete employee you will see the output is as expected because we make the id optional but the question is we only create one controller and two action methods and to process them we need to write two different routes in the real time application there might be hundreds to thousands of action methods will be there do we need to create routes for each of them and the answer is if you are ready to have generic urls and your seo team agrees with some common pattern seo here means search engine optimization and in real time application ranking of google bing or any other search engine plays a very vital role and seo team are really choosy about these urls so if you are okay then you can use the default routes it is extremely simple to implement default routes let's see that so just copy and paste the pattern let's name it default delete default controller and action within pattern let's type controller within curly braces slash action within curly braces and then id with quotient mark to make it optional within curly braces and that's it any request with correct controller and action name will route it to the correct path let's see that let's run the program and let's change the url to employ slash index which is our controller and action method and you can see the output is as expected next change it to employee slash delete employee and again correct routing if i route it to list of employee then this url also routes it to the index action method of employee controller and if i route it to the root url then it displays the page not found error so whenever end user lands to your website they generally came to your root path for example www.google.com or youtube.com anything so we need to handle that request as well so let's get back to visual studio so to provide default controller an action only thing which we need to do is to give the controller an action name so let's give controller as employee and action as index so this is what i want whenever end user visits our website we want them to display list of employee all right so let's run the program once again and now you can see we are getting the proper message so this is our conventional routing it's called conventional routing because it establish a convention for url path the first path segment controller is equal to employee maps to the controller name the second path segment action is equal to index maps to the action name and the third segment id question mark within curly braces is used for optional id the question in id question mark makes it optional id is used to map to the model entity as you can see we have added multiple routes here so we call them multiple conventional route using conventional routing with the default routes allows creating the app without having to come up with new url pattern for each action microsoft makes it really simple to route as per our convenience alright then if you have any queries related to the content of this video do ask me in comments till then thanks for watching [Music] you
Info
Channel: PRO Concepts
Views: 1,675
Rating: undefined out of 5
Keywords: asp.net core, asp.net core tutorial, asp.net core 5, asp.net core 5 tutorial, asp.net core mvc 5, asp.net core mvc 5 tutorial, asp net mvc, asp net mvc tutorial, dotnet core, asp.net mvc core tutorial, asp.net mvc core course, dot net core tutorial, dot net core mvc tutorial, .net core, Routing in asp.net core, routing in asp.net core mvc, ProConcepts, Pro Concepts, Amit, conventional routing in asp.net core, asp.net core mvc routing, routing in .net 5, amit singh rawat
Id: mIHhDHyPGuo
Channel Id: undefined
Length: 13min 39sec (819 seconds)
Published: Thu Dec 09 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.