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
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
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.
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.
Then run your application and navigate to "http://localhost:54085/Login"
Post a Comment
0 Comments