ASP.NET Podcast

ASP.NET Podcast is geared towards the Microsoft .NET Framework and ASP.NET.
The podcast is run by Wally McClure, David Penton, and Paul Glavich.
Welcome to ASP.NET Podcast Sign in | Join | Help
in Search

ASP.NET Podcast

The ASP.NET Podcast features, technical talks, interviews, news, reviews, and Wallyisms. Wallace B. (Wally) McClure, David Penton, and Paul Glavich are your hosts. We talk about ASP.NET, AJAX, Performance, Databases, WCF, Silverlight, Cloud Computing, Windows Azure, and whatever else we decide to talk about.

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 = (WAP.Routing.RoutingExample)BuildManager.CreateInstanceFromVirtualPath("~/Routing/RoutingExample.aspx", 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;
            }
        }
    }
}

Published Tuesday, October 07, 2008 2:35 PM by admin
Filed under: , ,
Anonymous comments are disabled

This Blog

Syndication

Powered by Community Server, by Telligent Systems