Routing in Asp.Net MVC with example

Routing in Asp.Net MVC with example

01 Apr 2024
Intermediate
240K Views
4 min read

Routing in Asp.Net MVC: An Overview

Basically, Routing is a pattern-matching system that monitors the incoming request and figures out what to do with that request. At runtime, the Routing engine uses the Route table to match the incoming request's URL pattern against the URL patterns defined in the Route table. You can register one or more URL patterns to the Route table at the Application_Start event. MVC5 also supports attribute routing, to know more refer to Attribute Routing in ASP.NET MVC.

In this MVC Tutorial, we will explore more about Routing in Asp.Net MVC which will include asp.net mvc routing in-depth, the workings of routing, the difference between routing and URL rewriting, routing vs URL rewriting. Consider our ASP.NET MVC Course for a better understanding of all MVC core concepts.

How to define a route in Asp.Net MVC...

public static void RegisterRoutes(RouteCollection routes)
{
 routes.MapRoute(
 "Default", // Route name
 "{controller}/{action}/{id}", // Route Pattern
 new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Default values for above defined parameters
 );
}

protected void Application_Start()
{
 RegisterRoutes(RouteTable.Routes);
 //To:DO
}   

When the routing engine finds a match in the route table for the incoming request's URL, it forwards the request to the appropriate controller and action. If there is no match in the route table for the incoming request's URL, it returns a 404 HTTP status code.

Note

Always remember route name should be unique across the entire application. The route name can’t be duplicated.

How it works...

In the above example, we have defined the Route Pattern {controller}/{action}/{id} and also provided the default values for controller, action, and id parameters. Default values mean if you will not provide the values for the controller or action or ID defined in the pattern then these values will be served by the routing system.

Suppose your web application is running on www.example.com then the url pattern for your application will be www.example.com/{controller}/{action}/{id}. Hence you need to provide the controller name followed by the action name and ID if it is required. If you will not provide any of the values then the default values of these parameters will be provided by the routing system. Here is a list of URLs that match and don't match this route pattern.

Matching URLs
Request URL
Parameters
http://example.com/
controller=Home, action=Index, id=none, Since the default values of controller and action are Home and Index respectively.
http://example.com/Admin
controller=Admin, action=Index, id=none, Since the default value of action is Index
http://example.com/Admin/Product
controller=Admin, action=Product, id=none
http://example.com/Admin/Product/1
controller=Admin, action=Product, id=1
http://example.com/Admin/Product/SubAdmin/1
No Match Found
http://example.com/Admin/Product/SubAdmin/Add/1
No Match Found

Difference between Routing and URL Rewriting

Many developers compare routing to URL rewriting that is wrong. Since both approaches are very much different. Moreover, both approaches can be used to make SEO-friendly URLs. Below is the main difference between these two approaches.

  1. URL rewriting is focused on mapping one URL (new URL) to another URL (old URL) while routing is focused on mapping a URL to a resource.

  2. URL rewriting rewrites your old URL to a new one while routing never rewrites your old URL to a new one but it maps to the original route.

Conclusion
So in this article, we have learned about routing in Asp.Net MVC with example. I hope you enjoyed learning these concepts while programming with Asp.Net. Feel free to ask any questions from your side. Your valuable feedback or comments about this article are always welcome. Level up your career in MVC with our ASP.Net Core Certification.

FAQs

Q1. Why routing is required in MVC?

It  is used for handling HTTP requests and searching matching action methods, and then executing the same

Q2. What is routing table in MVC?

The RouteTable is a global variable which is created when our MVC Application is started.

Q3. What is the main purpose of routing?

It helps minimize network failure by managing data traffic so that a network can use as much of its capacity as possible without creating congestion
Share Article
Batches Schedule
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Self-paced Membership
  • 22+ Video Courses
  • 750+ Hands-On Labs
  • 300+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Accept cookies & close this