Chapter 15. URL Routing Overview

URL routing gives your application the ability to handle request URLs that do not map directly to specific files. Routing can be used to define URLs that are more meaningful to users and to improve the Search Engine Optimization (SEO) for your application. In this lesson I provide an overview of routing in ASP.NET 4. I cover the details for using routing in Web Forms and MVC in Lessons 15A and 15B, respectively.

Without URL routing, the URL for a page that lists recipes by category looks like this:

http://localhost/recipes/list.aspx?category=soup

With URL routing, the URL for the same page looks like this:

http://localhost/recipes/category/soup

This new URL is both easier for people to use and for search engines to index. Also, this new URL does not expose the file structure of your web application to the users.

To enable routing in your application, your application must have at least one route registered in the global RouteTable. The global RouteTable is defined in the Global.asax file and is initialized during the Application_Start method in the Global.asax.cs file.

Each route is a member of the Route class. The Route class is used to specify how routing is processed in your web application. You need to create a Route object for each unique URL pattern that your application needs to handle. These are the properties of the Route class:

  • URL

  • Defaults

  • Constraints

  • DataTokens

  • RouteHandler

The URL property designates the URL pattern that the route will use. For example, ...

Get ASP.NET 4 24-Hour Trainer now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.