ASP.NET Routing

Description Article

ASP.NET routing enables to use URLs that do not have to map to specific files in a Web application.
Because the URL does not have to map to a file, the URLs in a Web application  can be descriptive of the user’s action and therefore more easily understood by users.

In ASP.NET routing, URL patterns that contain placeholders for values that are used when the
URL requests are handled at run time, the pieces of the URL that follow the application name are parsed into discrete values, based on a URL pattern that were defined.

When an ASP.NET application handles a request, the application iterates through the collection of routes in the Routes property to find the route that matches the format of the URL request. The order of the routes that you add to the Routes property is significant, because the application uses the first route that it finds in the collection that matches the URL.

How is it implemented

ASP.NET routing is a native ASP.NET HTTP module.

How it works

The URL patterns defined are known as routes. In a route, specified placeholders are mapped to values that are parsed from the URL request.
Constant values can be specified in order to matching the correct URL requests.

In a route definition:

  • Placeholders are defined (referred to as URL parameters) by enclosing them in braces ( { and } );
  • The “/character is interpreted as a delimiter when the URL is parsed;
  • Information in the route definition that is not a delimiter and that is not in braces is treated as a constant value;
  • Values that are extracted from between the delimiters are assigned to placeholders.

The following table shows valid route patterns and examples of URL requests that match the patterns:

Route definition Example of matching URL
{controller}/{action}/{id} /Products/show/beverages
{table}/Details.aspx /Products/Details.aspx
blog/{action}/{entry} /blog/show/123
{reporttype}/{year}/{month}/{day} /sales/2008/1/5
{locale}/{action} /en-US/show
{language}-{country}/{action} /en-US/show

Query Strings

When a route is chosen to map then the query string (if any) can be  later accessed on the route handler through the ”Request::QueryString method.

Where to define routes

Routes are defined in a method that is called from the handler for the Application_Start event in the “Global.asax” file.

This approach makes sure that the routes are available when the application starts.

The routes are defined by adding them to the static “Routes property of the “RouteTable class.
The “Routes” property is a “RouteCollection” object that stores all the routes for the ASP.NET application.


When routing handles URL requests, it tries to match the URL of the request to a route.
Matching a URL request to a route depends on all the following conditions:

  1. The route patterns defined or the default route patterns, if any, that are included in your project type;
  2. The order in which routes have been added them to the “Routes” collection;
  3. Any default values that were provided for a route;
  4. Any constraints that where provided for a route;
  5. Whether routing where configured to match a physical file.

 Routing classes


Class Description
Route Represents a route in a Web Forms or MVC application.
DynamicDataRoute Represents a route in a Dynamic Data application.
RouteBase Serves as the base class for all classes that represent an ASP.NET route.
RouteTable Stores the routes for an application.
RouteCollection Provides methods that enable you to manage a collection of routes.
RouteCollectionExtensions Provides additional methods that enable you to manage a collection of routes in MVC applications.
RouteData Contains the values for a requested route.
RequestContext Contains information about the HTTP request that corresponds to a route.
StopRoutingHandler Provides a way to specify that ASP.NET routing should not handle requests for a URL pattern.
PageRouteHandler Provides a way to define routes for Web Forms applications.
RouteValueDictionary Provides a way to store route Constraints, Defaults, and DataTokens objects.
Routing and Rewriting

ASP.NET routing differs from other URL rewriting modules:

  • URL rewriting processes incoming requests by actually changing the URL before it sends the request to the Web page.
  • With ASP.NET routing, the URL is not changed when an incoming request is handled, because routing can extract values from the URL.

When routing is not applied

By default, routing does not handle requests that map to an existing physical file on the Web server.

For example, a request for “http://server/application/Products/Beverages/Coffee.aspx” is not handled by routing if a physical file exists at “Products/Beverages/Coffee.aspx”.

To allow routing to handle all requests, even requests that point to files, the default behavior must be explicitly overrided by setting the “RouteExistingFiles” property of the “RouteCollection” object to true.


Enable routing

To enable routing mechanism ,the HTTP Redirection feature must be enabled for IIS Routing:

It must be installed on:

1. Open Control Panel / Programs / Turn Windows Features on or off

1.1  World Wide Web Services / Common HTTP Features . Check HTTP Redirection

Behavior on II6 and prior versions

By default extensionless patterns like “” will not be handled. This is because only “.aspx” and other ASP.NET-like extensions are handled by engine.

To enable routing for extensionless patterns the following steps must be followed in order to create a Wildcard Script Map. Refer to this article for details