In the previous posts, we’ve explored the internals of the routing assembly. If you’ve downloaded the ASP.NET MVC preview 3 from codeplex, then you’ll find an interesting sample on how to reuse the routing assembly with a ASP.NET web forms app.
If you’ve read the previous posts on the series, then you should know that a route is always associated with an IRouteHandler object which is responsible for returning the IHttpHandler that will handle the current request. The WebFormRouting assembly introduces several elements that you may reuse in your own web forms apps if you want to use the new routing features.
The IRoutablePage interface is added to the project to let you get access to the RequestContext generated internally by the routing assembly. This interface expands the IHttpHandler interface and adds a single property, called RequestContext, that will let you access the current RequestContext (RequestContext is an abstraction introduced by the abstractions assembly that ends up wrapping the current HTTP context).
Besides the interface, the WebFormRouting assembly will also add an existing RoutablePage class, which extends the Page class and implements the IRoutablePage interface. Besides that, it will also add a read only RouteData property that returns the RouteData associated with the current request.
There’s also one more important class introduced by the WebFormRouting assembly: the WebFormRouteHandler class. This class implements the IRouteHandler interface and you should associate it to a route when building web forms apps that use routing. The coolest thing about this class is that you can configure it to perform security checks on the requested url. When you enable security checks, you’ll end up using the AutorizationModule.CheckUrlAccessForPrincipal method for performing those verifications.
Finally, it’s also important to mention that the WebFormRouting assembly introduces several extension methods to the RouteCollection class that will reduce the code you need to write for entering new routes on a web forms app that reuses these classes.
Now that you’ve got a general idea on what the WebForRouting assembly does, you shouldn’t really have any difficulties understanding the WebFormRoutingDemoWebApp that comes with the futures sample. On the Global.asax file you’ll find several routes definitions. Here’s one:
In this case, the Backdoor url is being mapped to the ~/Admin/SecretPage.aspx and you’re not using security checks to see if the current user can access the resource (notice the false value beeing passed to the To extension method introduced by the WebFormRouting assembly).
If you look at all the pages, you’ll notice that most of them are just normal pages. If you need to get access to the current url parameter values, then you should inherit from RoutablePage instead of the traditional Page class. I believe that the only page that does that is the default.aspx page.
And that’s it! Integrating the new routing framework with a web app is as easy as reusing the WebFormRouting assembly! I only wished having this assembly a year and a half ago, when I had to implement url rewriting on one of the last web apps I developed…
On the next posts, I’ll start looking at the MVC framework. Keep tuned!