Now, in this ASP.NET Core tutorial, I will discuss Routing in ASP.NET Core. Routing basically a concept about how to send HTTP request to the right controller. If we say simply, Routing is a process of mapping a URL (Uniform Resource Locator) request to a specific controller action. Routing plays a vital role in the execution flow of ASP.NET Core application.
Previous ASP.NET Core tutorials
- Introduction to ASP.NET Core 2.0
- How to create a new project in ASP.NET Core 2.0 using Visual Studio.
- Project Folder structure and responsibility of each file in ASP.NET Core 2.0 Application.
- Middleware in ASP.NET Core
Let’s move ahead….
How Routing engine works in ASP.NET
Let’s understand how routing engine works in asp.net application. Basically, routing in asp.net is a pattern matching system which enables you to match the incoming request to a particular MVC action defined within the controller. In simple, if ASP.NET Routing engine receives a request, then It finds a match for that request URL. If any matches found then it forwards that particular request to the controller. Otherwise, it will return a 404 error Not Found page.
How to Setup Routing in ASP.NET Core Application.
Routing is dependent on the middleware pipeline. The middleware decides the given HTTP request should go to a controller or not go to a controller for processing. Using ASP.NET Core middleware, we define templates that tell MVC how to look at URL and then find a controller name and then an action name. As you know Controller is a C# class and the action is a method inside the controller.
When we create a new ASP.NET Core MVC project, the route is already added to that project. Here I will not create a new project. I will use that project which I have created in the previous tutorial.
So, go to solution explorer and open startup.cs file. In this file, you will see the UseMvc() pipeline middleware under the Configure method. And the only one route is defined under the app.UseMvc() middleware, which is the default route.
Let’s understand the code of route.
Name: “Default” => simply Name of the route.
template: “{controller=Home}/{action=Index}/{id?}” => It presents that URL will be in three parts. First part will be a controller, the second part will be the action of that controller, and the third part will be an optional parameter.
Note: – Whenever you see the URL in the form of {x}/{y}/{z}, then the first part will be a controller, the second one will be the action of that controller, and the third part will be an optional parameter.
So, according to the template. Our default controller will be home, action will be index and parameter will be optional.
Note: – The URL pattern is considered only after domain name part in the URL.
So, now run your application, you will see the URL in the form of http://localhost:65388/ and home page will be loaded.
Let’s see how routing engine maps the URL to the corresponding controller as:
- It looks the controller segment in URL. If this segment or part is empty then it takes the default controller “Home” as we have defined in the startup.cs class.
- Then it looks action segment in URL, if this segment or part is empty then it takes the default action “Index” as we have defined in the startup.cs class.
- Then Id parameter part will be ignored because we have defined the Id parameter is optional in the startup.cs class
It means, you can request any URL from the below list, you will see the same output page.
Custom Route in ASP.NET Core Application
If you want to add your own route you can do it easily. Let’s start step by step.
Step 1: Go to solution explorer => open the startup.cs file => then go to app.UseMvc() pipeline middleware. Then add the below code just above the default route.
Now run your application you will see the default route page. Then change your route with this “http://localhost:65388/student/Index”. Then you will HTTP 404 error page. It is because routing engine will see the student controller in your application. But your application does not have any student controller.
Step 2: Now, add a new controller in your project with the name of “student. So, go to solution explorer => right click on the controller folder => choose Add => then choose controller.
Step 3: Then choose MVC Controller – Empty option and then click Add button.
Step 4: Then enter the controller name like “studentController” and click Add button. It will open a new controller class with Index action method.
Now, right click on the Index action method and then choose Add View. It will open a new popup window. Now just click the Add button and the view will be added to the student folder under the views folder.
Now run the application with this route. At this time, you will see the Index view of student controller.
Leave a Reply