Please enable Javascript to correctly display the contents on Dot Net Tricks!

Routing in Asp.Net MVC with example

  Authors : Shailendra Chauhan
Updated On : 26 Sep 2016
Total Views : 164,501   
Support : MVC3 & MVC4

Basically, Routing is a pattern matching system that monitor the incoming request and figure out what to do with that request. At runtime, Routing engine use the Route table for matching 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 Application_Start event. MVC5 also supports attribute routing, to know more refer Attribute Routing in ASP.NET MVC.

How to define route...

public static void RegisterRoutes(RouteCollection routes)
 "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()

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.


Always remember route name should be unique across the entire application. Route name can’t be duplicate.

How it works...

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

Suppose your webapplication is running on then the url pattren for you application will be{controller}/{action}/{id}. Hence you need to provide the controller name followed by action name and id if it is required. If you will not provide any of the value then 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
controller=Home, action=Index, id=none, Since default value of controller and action are Home and Index respectively.
controller=Admin, action=Index, id=none, Since default value of action is Index
controller=Admin, action=Product, id=none
controller=Admin, action=Product, id=1
No Match Found
No Match Found

Difference between Routing and URL Rewriting

Many developers compare routing to URL rewriting that is wrong. Since both the approaches are very much different. Moreover, both the 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. Actually, URL rewriting rewrites your old url to new one while routing never rewrite your old url to new one but it map to the original route.

What do you think?

I hope you have got what is routing and how it works. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Free Interview Books
13 NOV
MEAN Stack Development (offline)

Sat, Sun (11:00 AM-12:30 PM IST)

Know More
PPC Marketing (offline)

Sat, Sun 09:00 AM-10:30 AM IST

Know More
ASP.NET MVC with AngularJS Development (offline)

SAT,SUN 08:00 AM-09:30 AM

Know More
Spoken English & Personality Development (offline)

Sat,Sun 05:00 PM-06:30 PM IST

Know More
NodeJS Development (online)

MON-FRI 06:00 AM - 07:30 AM IST

Know More
AngularJS Development (online)

MON-FRI 08:00 PM-09:30 PM IST.

Know More
27 OCT
ASP.NET MVC with AngularJS Development (online)

MON-FRI     07:30 AM-09:00 AM IST

26 OCT
ASP.NET MVC with AngularJS Development (online)

Mon-Fri     07:30 AM-09:00 AM IST

25 OCT
.NET Development (offline)

Mon-Fri     9:00 AM-11:00 AM IST

24 OCT
MEAN Stack Development (online)

MON-FRI     06:00 AM -07:30 AM IST

12 OCT
ASP.NET MVC with AngularJS Development (online)

Mon-Fri     09:30 PM-11:00 PM IST

17 SEP
ASP.NET MVC with AngularJS Development (online)

Sat, Sun     03:00 PM-05:00 PM IST

17 SEP
ASP.NET MVC with AngularJS Development (offline)

SAT,SUN     05:00 PM-06:30 PM IST

NodeJS Development (offline)

Sat, Sun     11:00 AM-12:30 PM IST

24 JUL
PPC Marketing (offline)

Sat,Sun     03:00 PM-05:00 PM


Professional Speaks