Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Understanding the Routing Framework in ASP.NET MVC

0.00/5 (No votes)
11 May 2014 1  
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:

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here