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.
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:
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.
- 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.
As a demo I have used a simple page with two hyperlinks as shown below:
In the Page_Load event I have retrieved the value passed as Route in the url as shown below:
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.
Комментариев нет:
Отправить комментарий