Mike Ormond has a nice post about how to use the Asp.Net routing capabilities of Asp.Net MVC, in your standard webforms sites.
In his example Mike goes through the essentials of how to set this up in Application_start and how to create and register your own IRouteHandler. Mike's example explains this really well with a simple example of routing a single parameter to a matching aspx page as follows:
I wanted to see how you can map multiple parameters to a single page and pass that parameter data to the page. So that the following mappings were made:
The first thing to do is to change the routes registered in global.asax, so that we have routes that expect our two parameters for username and area:
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
private void RegisterRoutes(RouteCollection Routes)
{
Route r = new Route("{username}/{area}" ,new SimpleRouteHandler());
Route r2 = new Route("{username}", new SimpleRouteHandler());
Routes.Add(r);
Routes.Add(r2);
}
Next we need to modify the SimpleRouteHandler to deal with these routes. At first I expected an overload on the CreateInstanceFromVirtualPath method that let me pass some parameters, but that was not available. Instead I simple added the RouteData (which contains the username and area parameters) to the Context Items collection:
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
string virtualPath = string.Format("~/default.aspx");
foreach (var value in requestContext.RouteData.Values)
{
requestContext.HttpContext.Items[value.Key] = value.Value;
}
return (Page)BuildManager.CreateInstanceFromVirtualPath(virtualPath, typeof(Page));
}
Now I can work with those values on default.aspx (and in this case just display them):
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = string.Format("username:{0} area:{1}", Context.Items["username"], Context.Items["area"]);
}
Cheers
Ian