четверг, 3 сентября 2009 г.

ASP.NET 4.0 WebForms Routing

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.

  1. <httpModules>
  2. <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
  3. <add name="RoutingModule" type="System.Web.Routing.UrlRoutingModule"/>
  4. 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 theApplication_Start event of Global.asax.cs as shown below:

  1. protected void Application_Start(object sender, EventArgs e)
  2. {
  3. RegisterRoutes();
  4. }
  5. private void RegisterRoutes()
  6. {
  7. RouteTable.Routes.Add("Products",new Route("{product}",new PageRouteHandler("~/Default.aspx")));
  8. }

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 IRouteHandleras argument.Here I have passed an instance ofPageRouteHandler which is an implementation of IRouteHandlerinterface specifically designed to handle of ASP.NET Webforms.
      • PageRouteHandler takes the relative path of the WebForm as input.

As a demo I have used a simple page with two hyperlinks as shown below:

  1. <form id="form1" runat="server">
  2. <div>
  3. <h2> Routing Demoh2>
  4. <ul>
  5. <li>
  6. <asp:HyperLink runat="server" NavigateUrl="/Books" Text="Books">asp:HyperLink>
  7. li>
  8. <li>
  9. <asp:HyperLink runat="server" NavigateUrl="/CDDVD" Text="CD and DVD">asp:HyperLink>
  10. li>
  11. ul>
  12. div>
  13. form>

In the Page_Load event I have retrieved the value passed as Route in the url as shown below:

  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. string product = RouteData.Values["product"] as string;
  4. if (product != null)
  5. {
  6. Response.Write("Product::" + product);
  7. }
  8. }

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.

Комментариев нет: