0% found this document useful (0 votes)
4 views18 pages

Understanding Conventional Routing in ASP.NET

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views18 pages

Understanding Conventional Routing in ASP.NET

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

What is Conventional Based Routing?

Conventional based routing is the traditional routing mechanism in which


routes are defined in the [Link] file.

The syntax to define Conventional Routing in [Link] Core MVC is given


below:
1. In the [Link] Core MVC Web Application, the controller action method
handles incoming HTTP Requests, i.e., URLs.

2. For example, if we issue a request to the /Home/Index URL, then the


Index action method of the Home Controller class will handle the
request, as shown in the image below.

3. Similarly, if we issue a request to the /Home/Details/2 URL, then the


Details action method of the Home Controller class will handle the
request, as shown in the image below. Here, the parameter value 2 is
automatically mapped to the id parameter of the Details action method.

4.

5. Now, the question that should come to your mind is, we have not
explicitly defined any routing rules for the application. Then how is this
mapping done, i.e., how is the /Home/Index URL mapped to the Index
action method, and how is the /Home/Details/2 URL mapped to the
Details action method of the Home Controller class?
6. This is done by the MVC Routing Middleware Component, which we
registered into the Request Processing Pipeline application. (if case we
use the [Link] core Web Mode-View_Controller Template).
(Conventional Routing ) Without MVC Template

Step => 1

Step => 2
Give Project name as Routing_Without_MVC , and then click on Next
Step => 3
Select .Net 6.0 Framework, and then click on Create .

Step => 4
o In this “[Link] Core Empty” Template we will not get the Folders like
Models, Views, Controllers , wwwroot….etc

o So we have to create them Explicitly.


Step 5: Creating Folders and Adding Controllers and Views

1. Create Folders for Models, Views, and Controllers:

o In the Solution Explorer, right-click on your project.


o Select Add -> New Folder.
o Name the folder Controllers.
o Repeat the above steps to create folders named Models and Views.

2. Add a Controller:

o Right-click on the Controllers folder.


o Select Add -> Controller.
o In the Add New Scaffolded Item dialog, select MVC Controller
- Empty.
o Click Add.
o Name the controller HomeController and click Add.

3. Create an Action Method in the Controller:

o Open the [Link] file.


o Add an action method that returns a view. For example:
4. Create a View for the Action Method:

o Right-click on the Index action method name in the HomeController.


o Select Add View.
o In the Add View dialog, ensure the following:
 View name: Index (This should match the action method name)
 Template: Empty (without model)
 View engine: Razor (CSHTML)
o Click Add.

5. Verify the View Location:


o The newly created view should be located in Views -> Home ->
[Link].
 Note: The Home folder is created automatically, corresponding to the
HomeController.

6. Adding Content to the View:


o Open the [Link] file in the Views/Home folder.
o Add some HTML content to verify it works:
7. In [Link] Empty Template the [Link] file doesn’t have any registered
services.

8. So Registered some Services in the [Link] file


9. Run the application
Important Points
(Conventional Routing ) With MVC Template

Step => 1

Step => 2
Give Project name as Routing_With_MVC , and then click on Next

Step => 3
Select .Net 6.0 Framework, and then click on Create .
Step => 4
o In this “[Link] Core web (Model -View-Controller)” Template we will
get the Folders like
Models, Views, Controllers , wwwroot….etc

o So no need to create them Explicitly.


Step => 5
Just make some changes in the heading of the [Link] and Run the
application

Step => 6 In this template services are added automatically in [Link]


Configure Multiple Conventional Routing in [Link] Core MVC
Application
1. Configuring Multiple Conventional Routes in an [Link] Core MVC
application involves defining multiple route patterns that the application
can use to handle incoming requests and map them to the appropriate
controllers and actions.

2. This is done in the [Link] class file of your [Link] Core


project. For example, we want to access the Index Action Method of the
Student Controller using the following URL.
[Link]

3. To achieve this, we can configure the MapControllerRoute method, as


shown in the image below. Here, you can see we have specified the
pattern as Student/All and the default controller and action name
as controller = Student, action = Index.

4. Next, we want to access the Details of the action method for the Student
Controller using the following URL.
[Link]

5. To achieve this, we can configure another MapControllerRoute method,


as shown in the below image. Here, you can see we have specified the
pattern as StudentDetails/{ID} and specified the default controller and
action name as controller = Student”, action = Details.
6. For the rest of the controllers and actions, we need to access them using
the following URL Pattern. We also need to configure the default
controller and action names as Home and Index.
[Link] Name}/{Action method Name}

7. To achieve this, we can configure another MapControllerRoute method,


as shown in the below image. Here, you can see we have specified the
pattern as {controller}/{action}/{id:int?} and specified the default
controller and action name as controller = Home, action = Index.
Difference

You might also like