Programming Microsoft ASP.NET 4 - Dino Esposito [85]
Category/{action}/{categoryName}
URLs that match the preceding route begin with the word “Category” followed by two segments. The first segment will be mapped to the action route parameter; the second segment will be mapped to the categoryName route parameter. As you might have guessed, action and categoryName are just arbitrary names for parameters. A URL that matches the preceding route is the following:
/Category/Edit/Beverages
The route is nothing more than a pattern and is not associated with any logic of its own. Invoked by the routing module, the component that ultimately decides how to rewrite the matching URL is another one entirely. Precisely, it is the route handler.
Technically speaking, a route handler is a class that implements the IRouteHandler interface. The interface is defined as shown here:
public interface IRouteHandler
{
IHttpHandler GetHttpHandler(RequestContext requestContext);
}
In its GetHttpHandler method, a route handler typically looks at route parameters to figure out if any of the information available needs to be passed down to the HTTP handler (for example, an ASP.NET page) that will handle the request. If this is the case, the route handler adds this information to the Items collection of the HTTP context. Finally, the route handler obtains an instance of a class that implements the IHttpHandler interface and returns that.
For Web Forms requests, the route handler—an instance of the PageRouteHandler class—resorts to the ASP.NET build manager to identify the dynamic class for the requested page resource and creates the handler on the fly.
Important
The big difference between plain URL rewriting and ASP.NET routing is that with ASP.NET routing, the URL is not changed when the system begins processing the request. Instead, it’s changed later in the life cycle. In this way, the runtime environment can perform most of its usual tasks on the original URL, which is an approach that maintains a consistent and robust solution. In addition, a late intervention on the URL also gives developers a big chance to extract values from the URL and the request context. In this way, the routing mechanism can be driven by a set of rewriting rules or patterns. If the original URL matches a particular pattern, you rewrite it to the associated URL. URL patterns are an external resource and are kept in one place, which makes the solution more maintainable overall.
Routing in Web Forms
To introduce URL routing in your Web Forms application, you start by defining routes. Routes go in the global.asax file to be processed at the very beginning of the application. To define a route, you create an instance of the Route class by specifying the URL pattern, the handler, and optionally a name for the route. However, you typically use helper methods that save you a lot of details and never expose you directly to the API of the Route class. The next section shows some code that registers routes.
Note
The vast majority of examples that illustrate routing in both ASP.NET MVC and Web Forms explicitly register routes from within global.asax. Loading route information from an external file is not be a bad idea, though, and will make your application a bit more resilient to changes.
Defining Routes for Specific Pages
In Application_Start, you invoke a helper method inside of which new routes are created and added to a static route collection object. Here’s a sample global.asax class:
public class Global : System.Web.HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("Category",
"Category/{action}/{categoryName}",
"~/categories.aspx",
true,
new RouteValueDictionary
{
{ "categoryName", "beverages" },
{ "action", "edit" }
});
}
}
All routes for the application are stored in a global container: the static Routes property of the RouteTable class. A reference to this property is passed to