Updated March 30, 2023
Introduction to ASP.NET MVC Routing
MVC means Model–View–Controller. It is an architectural pattern, which consists of three logical components i.e. the Model, the View, and the controller. These three components are built to handle specific development aspects of an application. To create scalable and extensible projects many companies nowadays are using it for industry-standard web development frameworks. MVC routing in ASP.NET is responsible for mapping incoming browser requests to the specific MVC Controller action. It has a routing table by which the application registers one or more patterns it. So that it will tell the routing engine what to do with any of the requests that match those patterns. This happens when we start the ASP.NET MVC Application.
Syntax:
The syntax for the MVC routing is, we need to add the below code in RouteConfig.cs File.
Routes.MapRoute(
Name:"Default",
url: "{controller}/{action}/{id}"
defaults: new { controller = "Home", action ="Index", id=UrlParameter.Optional}
URL patterns are the remaining part after the domain name. ASP.NET by default uses url pattern as “{controller}/{action}/{id}” which can be like localhost:2324 /{controller}/{action}/{id}. Anything, which is after “localhost2324”, will be considered as controller name. Anything after the controller name is action name and then its parameter which will be optional.
Default controllers and action will handle the request if the controller and actions are not specified. By default the controller will be HomeController and the action will be Index.
How to create an ASP.NET MVC Routing?
- We need to open visual studio and create a new MVC application.
- Open its RouteConfig.cs file and need to add the custom route.
- When we launces the MVC Application, Application_start() method is called.
- Route() method is responsible to call the route.config function.
- In the App_Start folder, we have Route.Config.cs File is present. We can define all the routes in this file. By default value is the Home controller – index method.
Let us see the default code below:
Routes.MapRoute(
Name:"Default",
url: "{controller}/{action}/{id}"
defaults: new { controller = "Home", action ="Index", id=UrlParameter.Optional}
- Code in line one contains Routes.MapRoute has attributes like name, url, and defaults like action, controller name, and id which is optional.
Have you ever wondered why it is useful and is used?
We all know if we search for the particular url sometimes we receive an error known as 404 page not found. This happens because in ASP.NET physical file location must have a match with the URL. If in case this doesn’t match then it will throw you the 404 error page not found.
With the help of routing, there is no need to file physically present to that particular location. If the file is not present in the directory then the browser will automatically search it in the entire directory of the project.
By using routing, suppose we only call the controller then it will by default called to a particular index which is related to that controller. The default ASP.NET MVC route is defined in the Global.asax file.
Examples of ASP.NET MVC Routing
It will be more clear with the help of an example. let us take a simple example.
Example #1
Suppose we want to route to “About” page of EDUCBA For that we need to create one MVC Application and open its RouteConfig.cs file.
We need to add its url, name, and controller in the code.
routes.MapRoute(
name: "about",
url: "educba.com/about-us"
defaults: new { controller = "educba.com", action = "about",
id = UrlParameter.Optional }
);
- Name is given as ‘about’ which is a route name.
- url is provided as “educba.com/about-us” which has its parameters.
- When we run the application automatically it will call the default “EDUCBA” controller.
- If a user hits the URL: ‘localhost:/educba.com/About-us’ it will invoke the Home controller ‘About’ Action Method.
Output:
Example #2
Another example is for the url having the date in it. Suppose we want to hit a below url for today’s blog.
URL: /Blog/2020/01/30/this-is-example-for-MVC
For this we need to write the route as given below:
Routes.MapRoutes("BlogArchive","Blog/{year}/{month}/{day}/{title}"
New { controller = "Blog", action = "List", month = "1", day = "1" },
New { year = @"\d{2}|\d{4}", month = @"\d{1,2}", day = @"\d{1,2}"}
);
Explanation:
This is mostly the WordPress URL that uses constraints to confirm whether we have one or two-digit date, a four-digit year, or a two-digit month.
Controller action will appear like this:
- public ActionResult Index(string year, string month, string day, string title)
- Any of the Curly brackets I the url are used to pass the controller’s designated action.
Important points of ASP.NET MVC Routing
Now we will discuss some important points regarding MVC Routing in ASP .NET.
- Routing plays a very vital role in the MVC framework. It maps url to the physical files or the class or the controller class of the MVC.
- URL pattern will start after the domain name. The route will contain a URL pattern and its information.
- The route method is used to create a route table.
- The route must be registered in the Application_Start event of Global.ascx.cs file.
- MVC routing is easy to edit or change if we want to change some parts of our code.
- It segregates our project into different segments, which makes it easier for the development and for the developers to work on it.
- It makes our project more systematic, less development, and less development cost.
- Here in MVC application, our code is already separated into three-part that is model, view, and controller.
- So we are not worried about the business logic and user interface.
Conclusion
By the end of this article, you all must get the knowledge of how the standard route table maps the requests to controller actions. In the end, we can say that the routing model is responsible for mapping browser requests, which is incoming to the specific MVC controller actions.
Recommended Articles
We hope that this EDUCBA information on “ASP.NET MVC Routing” was beneficial to you. You can view EDUCBA’s recommended articles for more information.