Click here to Skip to main content
15,881,089 members
Articles / Web Development / ASP.NET / ASP.NET4.0
Tip/Trick

Understanding the Routing Framework in ASP.NET MVC

Rate me:
Please Sign up or sign in to vote.
4.20/5 (7 votes)
11 May 2014CPOL2 min read 24.7K   6   2
Understanding the routing framework in ASP.NET MVC

Introduction

When anyone says the term "MVC", the first thing that appears in mind is three distinct components that make the whole MVC framework. These components are:

  1. Model
  2. View, and
  3. Controller

Although it was never mentioned anywhere in the name (MVC), routing framework is the first thing that actually came into play.

Why?

Although it is not mandatory, having knowledge of routing framework will surely give you a good understanding of the overall ASP.NET MVC framework. In the next post, I will discuss one of the coolest features of MVC5, i.e., [attribute] routing and this course would be a prerequisite for that tutorial.

Detail

Now as you have decided to continue reading this article, let's start exploring what the routing framework actually is. ASP.NET MVC Routing framework is the core of every ASP.NET MVC request. This means whenever an end user hits for a particular URL, the routing framework came into play.

Routing framework is basically a pattern matching system. The Routing System uses a series of rules listed in a routing table [App_Start\RouteConfig.cs] to determine which Controller and Action are executed in response to a request intercepted by the routing engine. When a URL is found to match a pattern, the routing engine attempts to match the text in the placeholders. If it cannot find a match, it returns a 404 HTTP Status Code. This whole concept is shown in the following image:

Please note, each routing rule contains placeholders that can match a controller, an action, and any number of variables. Variable data can be passed to the request handler without requiring a query string.

Now, let's have a look at the RouteConfig.cs file which is located under “App_Start” folder. Routing engine basically matches the pattern of the received url against the RouteCollection registered in the following file:

C#
public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
 
    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
   );
} 

As you can see, the default code snippet that ships with the MVC template is not only used for providing a Name and URL pattern but also defines a set of default parameters. In case the routing engine does not find a perfect match, it starts filling values that are not provided by the URL from the default values.

If you want to map new routes you simply add your desired routes in this file (RouteConfig.cs). You add the route to the application by adding the Route object to the static Routes property of the RouteTable class. The Routes property is a RouteCollection object that stores all the routes for the application.

Note

When adding new routes ALWAYS KEEP IN MIND that you have to add specific route on the top followed by more generic route in the end. Otherwise, your web app will never receive proper routing.

What's Next?

In the next post, I will discuss about “Attribute Routing”. Till then, have fun!

License

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



Comments and Discussions

 
GeneralMy vote of 5 Pin
Ali Murad25-May-14 21:10
Ali Murad25-May-14 21:10 
GeneralRe: My vote of 5 Pin
Shahriar Hossain25-May-14 21:13
professionalShahriar Hossain25-May-14 21:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.