ASP.NET Podcast Show #125 - Routing with Webforms - More Wally - Wallace B. McClure
in

MoreWally.com

Giving people what they want, More Wally. This is the technical and personal blog site of
Wallace B. (Wally) McClure.

This Blog

Syndication

Technical Sites

Archives

More Wally - Wallace B. McClure

This blog will have all kinds of posts about Wally McClure. In it, there will be tons of .NET and computer programming posts as well as Wally's views on life in general. As you might guess, this site and blog help you get More Wally in your life. What more could anyone want? iPhone, Android, Xamarin.iOS, Xamarin.Android, Mobile, HTML5, .NET, ADO.NET, ASP.NET, AJAX, jQuery, jQuery Mobile, ASP.NET AJAX, and Windows Azure............follow me on twitter at Wally

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

Published Oct 08 2008, 04:34 PM by wallym
Filed under: , ,

Comments

 

ScottGu's Blog said:

Here is the latest in my link-listing series .&#160; Also check out my ASP.NET Tips, Tricks and Tutorials

October 10, 2008 4:55 AM
 

Mirrored Blogs said:

Here is the latest in my link-listing series .&#160; Also check out my ASP.NET Tips, Tricks and Tutorials

October 10, 2008 5:01 AM
 

BusinessRx Reading List said:

Here is the latest in my link-listing series .&#160; Also check out my ASP.NET Tips, Tricks and Tutorials

October 10, 2008 5:05 AM
 

Joycode@Ab110.com said:

【原文地址】 October 10th Links: ASP.NET, ASP.NET AJAX, jQuery, IIS 【原文发表日期】 Friday, October 10, 2008 1:53

October 10, 2008 5:49 PM
 

Enlaces del 10 de Octubre: ASP.NET, ASP.NET AJAX, jQuery, IIS « Thinking in .NET said:

Pingback from  Enlaces del 10 de Octubre: ASP.NET, ASP.NET AJAX, jQuery, IIS &laquo; Thinking in .NET

October 12, 2008 5:00 AM
 

ASPInsiders said:

One of the topics I have been covering in my training events has been ASP.NET Routing.&#160; Routing

October 14, 2008 3:06 AM
 

Routing in ASP.NET 3.5 « butaji said:

Pingback from  Routing in ASP.NET 3.5 &laquo; butaji

October 14, 2008 10:54 AM
 

Mega Developer Blog said:

Normal 0 false false false EN-US X-NONE X-NONE MicrosoftInternetExplorer4 http://morewally.com/cs/blogs

October 20, 2008 1:57 PM
 

starspace said:

November 5, 2008 11:49 AM
 

Peter's Software House said:

I followed the great samples provided by Phil Haack and Wally McClure , but Phil’s sample required that

March 23, 2009 7:09 PM
2006 - Wallace B. McClure
Powered by Community Server (Non-Commercial Edition), by Telligent Systems