Follow me on Twitter at Twitter.com/wbm
FYI, I'm blogging most of my stuff over at More Wally now.
You might want to add my rss feed to your reader at:http://morewally.com/cs/blogs/wallym/rss.aspx
ASP.NET Podcast Show #125 - Routing with Webforms - Wallace B. McClure

Wallace B. McClure

All About Wally McClure - The musings of Wallym on Web, HTML5, Mobile, Xamarin.iOS, Xamarin.Android, and Windows Azure.

News

Visual Studio Magazine Column Personal Blog

.NET

Book Authors

Business

Family

Friends

Georgia Tech Bloggers

Personal

Archives

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;
            }
        }
    }
}

Comments

websparc said:

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?

# October 20, 2008 11:17 AM