Routing in MVC

Routing in MVC

 Hello Everybody,

In this blog we will discuss about Routing in ASP.NET MVC. Previously we discussed about how to Create Controller and View and Master Page in ASP.NET MVC.

https://webdevelopmentknowledge.blogspot.com/2020/10/creating-controller-and-view-in-aspnet.html


1


What is Routing?

Routing is pattern matching mechanism used to match the URL pattern with the Route table and call specific action method based on URL request. Based on the URL MVC application will decide which action method to be executed.


Example: http://localhost:54085/Login/Index -URL

                   http://localhost:54085 -Domain

                   Login - Controller Name

                   Index - Action Method Name


                    So when this URL is requested MVC application looks into route table for this request and it will look for controller name like Login and Action method name like Index.

Routing in MVC

Quickly open the project we have created in our previous blogs or create a new asp.net mvc web application.

by default Route config file will be their in your project.

Go to application folder -> App_Start -> RouteConfig.cs

2

Route Config is registered in Global.asax


when ever project is run application will create routetable based on the gobal.asax page registeration

This is the configuration class for routes. If you open this.

Routing in MVC is 2 Types :

1. Conventional based Routing

2. Attribute Routing

1.Conventional based Routing:

Default Route will be added as shown below.


3

For Route Configuration you have name, URL, defaults to add to Route Table.

a. name represents the route name

b. URL represents the URL pattern you want to define

c. defaults represents what is the default controller and action to open if the URL pattern matches this        URL pattern.


Default MVC will provide pattern like URL: "{controller}/{action}/{id}"

                                                                       "Login/Index" - id is optional

                                                                        "Home/Index" .


You can create your own way of URL pattern defined.

Here I created a Custom Route Name as Login and URL pattern as Login/{id} 

 Default I given Login controller and index action method in that.

4


Then run your application and navigate to "http://localhost:54085/Login"

5


2. Attribute Routing:

        In Real time project most of the people use Attribute Routing in Web API and MVC applications. It is easy way of defining name for this routing it is dynamic and changes name dynamically anytime. Routing in MVC
 It is available in MVC5. To enable Attribute routing go to RouteConfig.cs file and add below line.

routes.MapMvcAttributeRoutes(); 

6

In Attribute Routing we define RoutePrefix or Route in Controller level or Action method level.

Example for Controller level and action method level is as shown below.
  
7
Run project and test the URL: "http://localhost:54085/Login/RegisterPage"

8




Example for Routing only under action method level is as shown below.
   
9

1.1
 Now we understood how the Routing works in ASP.NET MVC. 
In Next blog we will discuss about how to pass data from Controller to View and Controller to Controller.

Thanks for reading if any doubts feel free reach out to me.

Post a Comment

0 Comments