Hello friends, I hope you all are doing great. In today's tutorial, we will have a look at URL Routing in ASP.NET Core. It's our 11th tutorial in ASP.NET Core series and it's an important but quite easy concept to understand.
You must be wondering how we are routing rite now as we haven't added any routing codes yet. But if you remember, we have used MVC with default route middleware in our pipeline and this middleware automatically sets up this default URL routing for our web application. So, let's understand how this URL Routing works in ASP.NET Core:
URL Routing in ASP.NET Core
URL Routing in ASP.NET Core is used to provide the controller's action method to the incoming HTTP request.
In simple words, when a user enters an HTTP URL in its browser, then it is greeted by the respective controller's method and which controller's method should by executed, it is decided by the URL Routing.
As you can see in below figure that our controller class has two action methods in it i.e. Index & info:
By default, first segment of URL ( home in our case ) is the name of the Controller and the second segment is the name of the action method. So, when we have info in the second segment then Info action method will be executed.
So, now let's understand this third segment in URL link, which is routed as a parameter to the action method.
If you remember, when we created the Info action method then we have hard coded the value of Engineers to 1.
So, let's make it flexible and ask the user to enter it as a third segment in the URL.
Open your Controller's file and make these changes to Info action method, as shown in figure on right side.
I have added a parameter int id in our Info method and then placed this id in the GetEngineers function.
So, now run your function and navigate to /home/info/3 and now you will get the data of your third engineer, as shown in below figure:
Now, we have automated our Info method by adding this third parameter and we can view any engineer's data quite easily.
But, if you open /home/info/ without the third parameter, then you will get a run time error and one way to avoid it is by making this third parameter null-able, as shown in figure on right side.
I have made the id null-able and then assigned it a default value of 1 by using id??1.
So, now if you don't add the third parameter then it will show you the data of first engineer by default.
This default routing of ASP.NET Core is also called Conventional Routing.
I hope you have understood the Conventional URL Routing in ASP.NET Core, so that was all for today. In the next lecture, we will have a look at How to include Bootstrap in ASP.NET Core. Till then take care & have fun !!!
syedzainnasir
I am Syed Zain Nasir, the founder of The Engineering Projects (TEP). I am a
programmer since 2009 before that I just search things, make small projects and now I am sharing my
knowledge through this platform. I also work as a freelancer and did many projects related to
programming and electrical circuitry. My Google Profile+Follow
Get Connected
Comments on ‘’ URL Routing in ASP.NET Core ‘’ ( 0 )