Those who have been working in ASP.NET MVC are already aware of the ASP.NET Routing feature introduced with .NET 3.5 SP1.But I have seen many who has developed a perception that ASP.NET Routing is something that can be used with only ASP.NET MVC.This is not the case.ASP.NET Routing comes under a separate namespace (System.Web.Routing) and assembly (System.Web.Routing.dll).This can be used with WebForms applications as well.But this required some custom coding to develop your RouteHandler as shown in the post below:
http://haacked.com/archive/2008/03/11/using-routing-with-webforms.aspx
In ASP.NET 4.0, added capabilities are provided to make routing fully complete in order to work with WebForms.This is what I will be discussing in this post today.I will use simple webpage with 2 hyperlinks to demonstrate this feature.
Step1: Add a reference to System.Web.Routing.dll to the ASP.NET Web Application project.
Step2: Add the UrlRoutingModule in the web.config as shown below.This HttpModule intercepts the incoming request and routes the control to appropriate handler.
<httpModules> <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add name="RoutingModule" type="System.Web.Routing.UrlRoutingModule"/> </httpModules>
Step3:We have to define the Routes and populate the RouteTable.RouteTable class exposes a static property Routes which is a collection of Route objects.This RouteTable is referenced by the ASP.NET Routing Engine to resolve the route values from the URL.The RouteTable needs to be populated at the Application_Start event of Global.asax.cs as shown below:
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes();
}
private void RegisterRoutes()
{
RouteTable.Routes.Add("Products",new Route("{product}",new PageRouteHandler("~/Default.aspx")));
}
Here Routes.Add method expects two parameters:
- A string identifying the name of the Route.In this case I have given “Products”
- An instance of the RouteBase class.This is the base class and I have passed an instance of the Route class which is an implementation of RouteBase.
- Route class takes url pattern and an instance of IRouteHandler as argument.Here I have passed an instance of PageRouteHandler which is an implementation of IRouteHandler interface specifically designed to handle of ASP.NET Webforms.
- PageRouteHandler takes the relative path of the WebForm as input.
- Route class takes url pattern and an instance of IRouteHandler as argument.Here I have passed an instance of PageRouteHandler which is an implementation of IRouteHandler interface specifically designed to handle of ASP.NET Webforms.
As a demo I have used a simple page with two hyperlinks as shown below:
<form id="form1" runat="server"> <div> <h2> Routing Demo</h2> <ul> <li> <asp:HyperLink runat="server" NavigateUrl="/Books" Text="Books"></asp:HyperLink> </li> <li> <asp:HyperLink runat="server" NavigateUrl="/CDDVD" Text="CD and DVD"></asp:HyperLink> </li> </ul> </div> </form>
In the Page_Load event I have retrieved the value passed as Route in the url as shown below:
protected void Page_Load(object sender, EventArgs e)
{
string product = RouteData.Values["product"] as string;
if (product != null)
{
Response.Write("Product::" + product);
}
}
RouteData is a new property that is exposed by the Page class and contains the route names and values.
This is a very rudimentary working sample.In the next post we will take a more detailed look into what PageRouteHandler class is doing.


[...] the rest here: ASP.NET 4.0 WebForms Routing – Part I « Sankarsan's Journal SHARETHIS.addEntry({ title: "ASP.NET 4.0 WebForms Routing – Part I « Sankarsan's Journal", [...]
[...] the original here: ASP.NET 4.0 WebForms Routing – Part I « Sankarsan's Journal SHARETHIS.addEntry({ title: "ASP.NET 4.0 WebForms Routing – Part I « Sankarsan's Journal", [...]
[...] This post was Twitted by Dave_Bush [...]
[...] http://sankarsan.wordpress.com/2009/08/09/asp-net-4-0-webforms-routing-part-i/ [...]