Monday, September 3, 2012

starting to learn web forms routing

Attempting to mimic this example, I made a new web forms project in Visual Studio and I made the Web.config end like so:

      <httpModules>
         <add name="RoutingModule" type="System.Web.Routing.UrlRoutingModule,
               System.Web.Routing, Version=3.5.0.0, Culture=neutral,
               PublicKeyToken=31bf3856ad364e35"/>
      </httpModules>
   </system.web>
   <system.webServer>
      <modules runAllManagedModulesForAllRequests="true">
         <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule,
               System.Web.Routing, Version=3.5.0.0, Culture=neutral,
               PublicKeyToken=31BF3856AD364E35" />
      </modules>
      <handlers>
         <add name="UrlRoutingHandler" preCondition="integratedMode" verb="*"
               path="UrlRouting.axd" type="System.Web.HttpForbiddenHandler,
               System.Web, Version=2.0.0.0, Culture=neutral,
               PublicKeyToken=b03f5f7f11d50a3a" />
      </handlers>
   </system.webServer>
</configuration>

 
 

I wrote this class which inheirts from IRouteHandler:

using System.Web;
using System.Web.Compilation;
using System.Web.Routing;
using System.Web.UI;
namespace WebFormsRoutingExample.Objects
{
   public class MyCustomRouteHandler : IRouteHandler
   {
      private string _virtualPath;
      
      public MyCustomRouteHandler(string virtualPath)
      {
         _virtualPath = virtualPath;
      }
      
      public IHttpHandler GetHttpHandler(RequestContext requestContext)
      {
         var display = BuildManager.CreateInstanceFromVirtualPath(_virtualPath,
               typeof(Page)) as IHttpHandler;
         return display;
      }      
   }
}

 
 

Global.asax.cs looks like this:

using System;
using System.Web.Routing;
using WebFormsRoutingExample.Objects;
namespace WebFormsRoutingExample
{
   public class Global : System.Web.HttpApplication
   {
      void Application_Start(object sender, EventArgs e)
      {
         RegisterRoutes();
      }
      
      void Application_End(object sender, EventArgs e)
      {
      }
      
      void Application_Error(object sender, EventArgs e)
      {
      }
      
      void Session_Start(object sender, EventArgs e)
      {
      }
      
      void Session_End(object sender, EventArgs e)
      {
      }
      
      private static void RegisterRoutes()
      {
         RouteTable.Routes.Add("EasyTest", new Route("foo/{bar}", new
               MyCustomRouteHandler("~/TryToGetToMe.aspx")));
      }
   }
}

 
 

So far I have been able to get routes that start with "foo" to route to TryToGetToMe.aspx, but I have had less luck in passing the bar variable denoted in curly brackets. I was unable to mimic the interface-inheirting-from-interface trickery that the example I copied from used. More soon.

No comments:

Post a Comment