FlyPaper

the sticky side goes up…

   Apr 04

Down and Dirty Routing

So you want to make yourself a .Net blog engine or something nice (PURL anyone)

that converts this: http://my.site.com/somepage/someVal  into something like http://my.site.com/somepage.aspx?somevar=someVal 

ASP.net routing is pretty daunting.  lotsa junk out there and whatnot.  Here’s the bigger deal though.  It’s not that hard…

There are basically 3 steps. all of which can be gleaned from here

Step 1:  Add the route to the Global.asax

public static void RegisterRoutes(RouteCollection routes) {
        routes.Add(new Route(
             "{Location}/{Name}"
             , new PGIRouteHandler("~/Public/Main.aspx")
                  ));
}

Step 2:  Create the routing handler (this moves the data into the page variables)

this baby should go somewhere in your namespace but doesn’t necessarily have to leave the current path. (i like to keep my code a little organized)


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

namespace Web.Code {
    public class PGIRouteHandler: IRouteHandler {

        public PGIRouteHandler(string virtualPath) {
            this.VirtualPath = virtualPath;
         }

        public string VirtualPath { get; private set; }

        public IHttpHandler GetHttpHandler(RequestContext rc) {

            foreach (var urlParm in rc.RouteData.Values) {

                //this is the bit that adds the info to the HttpContext.Items object
                rc.HttpContext.Items[urlParm.Key] = urlParm.Value;
            }
            var page = BuildManager.CreateInstanceFromVirtualPath
                    (VirtualPath, typeof(Page)) as IHttpHandler;

            return page;
        }

    }
}

Step 3:  Add the code to the Page_Load of the target page (Public/Main.aspx in this case) 

public partial class Main : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
            var Location = HttpContext.Current.Items["Location"];
            var Name = HttpContext.Current.Items["Name"];
        }
    }

 

That’s about the size of it.  This is really the fast way to the prize and in no way handles any security concerns that may arise. 

 

Happy Coding.


   Mar 28

Fly Paper + Clock * Science = FlyPoweredClock

So, this is the flypaper blog.  and here’s a nifty use of such paper.

image

Fly Powered Clock


   Mar 25

KOTD – the first.

Photo0395

This is where it all begins.


   Mar 25

KOTD – This should have been posted days ago…

So it’s Friday, at least currently, and there is no reason not to have this post now. dailyJustin


   Mar 25

Personally, I see no reason not to.

So I think I will be attempting to update this blog daily.  or at least nearly daily.  probably not on the weekends.

 

For today, I am looking at Kinect Hacks,  which will allow me to take control of something at some point.  what and when i don’t know.  Probably a robot bakery,  or a zombie killing machine.

EOF


   Mar 14

KOTD: Do you want me to write the code too?

dailyJustin

There’s a good reason for design.   I don’t really recall what it is.  but i know its out there.


   Feb 11

KOTD: Beer Fridays anyone?

dailyJustin

When I was a kid, I used to see a lot of scotch drinking at work in the movies and on T.V.   I think America was in a better place in those days,  people seemed to be more interested in their jobs and working hard.   Maybe it was because they were all loaded…


   Feb 10

Today I predict the end!

dailyJustin


   Feb 09

The Daily Kalb

dailyJustin


   Mar 17

Staying in the Base Path.

It’s not all about baseball but base paths can be a pain.  Here’s a simple way to mange the base paths and help to keep code simple and document relative.  Especially helpful when using Server.Transfer to move people to different pages in your site.

//This goes in the <head> section of your Master Page 
<base href="<%= BaseUrl %>Public/" />
 //this goes in the Maste Page Code Behind    
public static string BaseUrl
{
    get { string basePath = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority)  + HttpContext.Current.Request.ApplicationPath;
    if (!basePath.EndsWith("/")) basePath = basePath + "/";
    return basePath;
    }
}