Click here to Skip to main content
15,886,790 members
Articles / Web Development / ASP.NET

Understanding Routing in ASP.NET MVC

Rate me:
Please Sign up or sign in to vote.
3.13/5 (4 votes)
25 Mar 2014CPOL3 min read 21.9K   8  
Understanding routing in ASP.NET MVC

Routing plays an important role in an ASP.NET MVC Application execution flow. Basically, it maps request URL to a specific controller action using a Routing Table.

In order to describe user's actions, MVC framework uses friendly URLs against actions instead of mapping it to physical files as in the case of an ASP.NET Web Form application. In a typical ASP.NET Web Form application, request is mapped to a physical file as follows:

C#
//Displaying all employees
http://locahost:XXXX/Employee.aspx

//Displaying employee by Id
http://locahost:XXXX/Employee.aspx?Id=10

As opposed to the above approach, MVC framework maps request URL to controller as follows:

C#
//Displaying all employees
http://locahost:XXXX/Employee/

//Displaying employee by Id
http://locahost:XXXX/Employee/10/

In the above example URL, "Employee" is a Controller. The point to understand is how is this mapping performed in ASP.NET MVC framework?

For mapping URLs to controller class actions, MVC framework uses a Routing Engine. We can define Routing Rules for the engine, so that it can map incoming URLs to appropriate controller but default routing is already there for a newly created MVC application using Visual Studio. If we create a new ASP.NET MVC application in Visual Studio and run it, we will have default page like the following: Routing in ASP.NET MVC

Routing Engine uses routing rules that are defined in Global.asax file in order to parse the URL and find out the path of corresponding controller. We can find the following piece of code in Application_Start() method of Global.asax file.

C#
protected void Application_Start()
{
01.    AreaRegistration.RegisterAllAreas();
02.    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
03.   RouteConfig.RegisterRoutes(RouteTable.Routes);
04.    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

Line 03 in the above code snippet represents that routes are configured on application start by calling a static method "RegisterRoutes" in RouteConfig class.

We can find RouteConfig.cs file under App_Start folder in ASP.NET MVC 5 application. If we follow this method in RouteConfig class, we will find one default configured route as follows. Line # 03 to #07 is configuring one default route.

C#
public static void RegisterRoutes(RouteCollection routes)
{
01.   routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
02.
03.  routes.MapRoute(
04.      name: "Default",
05.      url: "{controller}/{action}/{id}",
06.      defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
07.      );
}
  • Line 04 => is the name for the route
  • Line 05 => represents that URL will be our controller, then action followed by id (if any)
  • Line 06 => default controller will be Home, default action will be Index and Id is optional

In the above case, Routing Engine maps URL "http://localhost:11517/" to corresponding controller as:

  • It looks for Controller Segment in URL which is empty, so it takes the default controller i.e. "Home".
  • Then it looks for Controller Action which is also not there, so it again takes the default controller action i.e. "Index"
  • Id is not provided and it's optional, so Routing Engine will ignore it.
Finally URL Engine evaluates something as "http://localhost:11517/Home/Index", where Home is our controller and Index is its Action. So, request will be forwarded to Home Controller's Index Action. Because of the default routing rule, request is forwarded to the same controller action in all following cases:
  • http://localhost:11517/
  • http://localhost:11517/Home
  • http://localhost:11517/Home/Index
But if we wanted to access some other controller, say "Employee", we will provide it explicitly as:
  • http://localhost:11517/Employee
  • http://localhost:11517/Employee/Index

Routing is a key step in MVC request executing cycle. The purpose of this web development tutorial is to provide a basic understanding of Routing in ASP.NET MVC application.

Other Recommended Web Application Development Articles

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Emaratech
United Arab Emirates United Arab Emirates
Imran Abdul Ghani has more than 10 years of experience in designing/developing enterprise level applications. He is Microsoft Certified Solution Developer for .NET(MCSD.NET) since 2005. You can reach his blogging at WCF Tutorials, Web Development, SharePoint for Dummies.

Comments and Discussions

 
-- There are no messages in this forum --