ASP.NET Podcast Show #125 - Routing with Webforms

Subscribe to All!

Subscribe to WMV.

Subscribe to M4V (iPod).

Subscribe to MP3.

Download WMV.

Download M4V (iPod).

Download MP3.

Show Notes:

  • Not just for MVC.
  • Available with .NET 3.5 SP1.
  • System.Web.Routing.
  • Web.config.
  • Global.asax.
  • Routing class.
  • Security.
  • Output page.

Source Code:

Web.config entry:

<httpModules>
.......
<add name="Routing" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</httpModules>

Global.asax:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.Security;
using System.Web.SessionState;

.................

        void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            string strUrlPat = String.Empty;
            Route rteRoute, rte2;

            strUrlPat = "{controller}/{action}/{nameid}";

            rteRoute = new System.Web.Routing.Route(strUrlPat, new DisplayHandler());
            System.Web.Routing.RouteTable.Routes.Add(rteRoute);

            strUrlPat = "Book/{ISBNid}";
            rte2 = new Route(strUrlPat, new BookHandler());
            RouteTable.Routes.Add(rte2);

        }

Handler class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Web;
using System.Web.Compilation;
using System.Web.Routing;
using System.Web.Security;
using System.Web.UI;

namespace WAP
{
    public class DisplayHandler : IRouteHandler
    {
        public IHttpHandler GetHttpHandler(RequestContext rc)
        {
            var routingPage = __LongTerm__ typeof(Page));
            routingPage.ControllerValue = rc.RouteData.Values["controller"] as string;
            routingPage.ActionValue = rc.RouteData.Values["action"] as string;
            routingPage.ProductValue = rc.RouteData.Values["nameid"] as string;
            return (routingPage);
        }
    }

    public class BookHandler : IRouteHandler
    {
        public IHttpHandler GetHttpHandler(RequestContext rc)
        {
            string VirtualPath = "~/Routing/Books.aspx";
            if (!UrlAuthorizationModule.CheckUrlAccessForPrincipal(VirtualPath,
                rc.HttpContext.User,
                rc.HttpContext.Request.HttpMethod))
                throw (new SecurityException());
            var routingPage = (WAP.Routing.Books)BuildManager.CreateInstanceFromVirtualPath(VirtualPath, typeof(Page));
            routingPage.ISBNNumber = rc.RouteData.Values["ISBNid"] as string;
            return (routingPage);
        }
    }
}

Codebehind page:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WAP.Routing
{
    public partial class RoutingExample : System.Web.UI.Page
    {
        public string ControllerValue { get; set; }
        public string ActionValue { get; set; }
        public string ProductValue { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                lblController.Text = ControllerValue;
                lblAction.Text = ActionValue;
                lblProduct.Text = ProductValue;
            }
        }
    }
}

1 Comment

  • I wonder how you able to compile that code. FIrst i not find anything like __LongTerm__ typeof.
    second when you add namespace to the aspx page class i cant access it in DisplayHandler class. Do you have working code for this example?

Comments have been disabled for this content.