MSDN Documentation

.NET | ASP.NET

Introduction to ASP.NET Routing

ASP.NET Routing is a powerful feature that allows you to define custom URLs for your web applications. Instead of relying on traditional file-based URLs (like /Products.aspx?id=123), routing enables you to create user-friendly and SEO-friendly URLs (like /products/123).

This system maps incoming requests to specific handler methods or controllers within your application, providing a flexible way to structure your application's navigation and resource access.

Why Use Routing?

  • SEO Friendly URLs: Create cleaner, more descriptive URLs that search engines can easily understand and index.
  • User Friendly URLs: Provide intuitive and memorable URLs for your users.
  • Decoupling URL from File Structure: Your URL structure no longer needs to mirror your physical file system.
  • API Development: Essential for building RESTful services and APIs where URL structure is key to resource identification.
  • Version Control: Allows you to evolve your application's URL structure without breaking existing links.

Core Concepts

Routes

A route is a definition that maps a URL pattern to a handler. Each route typically consists of:

  • A URL template (e.g., Products/{id})
  • Optional default values for route parameters
  • Optional constraints for route parameters

When a request comes in, the routing engine tries to match the request URL against the defined route templates. If a match is found, the corresponding handler is invoked.

Route Table

The route table is a collection of all the routes defined in your application. The order of routes in the table is crucial, as the engine processes them sequentially and uses the first match found.

Route Handlers

A route handler is responsible for processing a request that has been successfully matched to a route. In ASP.NET MVC, this is typically an IControllerFactory and a controller action. In ASP.NET Web Forms, it might be a custom IRouteHandler that invokes a specific page or component.

A Simple Example

Consider a basic route definition for displaying product details:


// In ASP.NET MVC (simplified C#)
routes.MapRoute(
    name: "ProductDetail",
    url: "products/{id}",
    defaults: new { controller = "Products", action = "Details", id = UrlParameter.Optional }
);
                

With this route, a URL like /products/123 would:

  1. Match the "products/{id}" URL template.
  2. Extract "123" as the value for the id parameter.
  3. Invoke the Details action on the Products controller, passing id = 123.
Tip: Properly defining your route table and route patterns is fundamental to building scalable and maintainable ASP.NET applications.

Evolution of Routing

Routing has evolved significantly across different ASP.NET versions and frameworks:

  • ASP.NET Web Forms: Initially, routing was introduced as an add-on to support cleaner URLs, often using System.Web.Routing.
  • ASP.NET MVC: Routing is a core component and integral to the MVC pattern.
  • ASP.NET Core: Routing is highly flexible and configurable, supporting middleware-based pipelines and extensive customization.

This documentation will focus on the fundamental principles that apply across many ASP.NET versions, with examples often drawing from modern ASP.NET implementations.

Continue to the next section to dive deeper into Route Tables.