Gal Ratner
Gal Ratner is a Techie who lives and works in Los Angeles CA and Austin TX. Follow galratner on Twitter Google
PageRouteHandler and URL patterns

PageRouteHandler resides in System.Web.Routing and implements IRouteHandler. It is designed to be a basic handler for Routes defined in an application, eliminating the need to implement custom IRouteHandlers when there are no business rules to validate. It can also check for access to physical files, and is a good quick solution for basic routing. A little known and not documented feature of PageRouteHandler is the ability to use Route style URL patters in its VirtualPath.  URL patters are placeholders enclosed by braces and separated by a literal value, for example: {language}-{country}/{action}. Routes will recognize the URL pattern and use the appropriate Page Route.


Let’s look at how to use URL patterns in our PageRouteHandler. In this example I will assume all the user wants to do is to universally route any requests for pages to the directory “pages”.
In Global.asax register the new route:

void Application_Start(object sender, EventArgs e) 
{
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.Add(new Route("{page}"new PageRouteHandler("~/pages/{page}")));
}

That’s about it. Quick and easy. Pages will be served from the pages directory with no need for any custom handlers.


Posted 7 Sep 2010 6:01 PM by Gal Ratner
Filed under: , ,

Powered by Community Server (Non-Commercial Edition), by Telligent Systems