Tuesday, October 12, 2004

Adventures In Url Rewriting

A project I'm currently working on for my employer using ASP.NET 1.1 requires a coherent mechanism to maintain a consistent look and feel throughout the entire site. Now, as ASP.NET 2.0 isn't here yet (Master Pages would have been a perfect fit for this project) I decided to use a mechanism based on URL rewriting.

In short, all the main 'page' functionality is contained within User Controls. There is a single .aspx page that loads the required user control and instantiates it for display within the HTML template. Using a regular expression based url rewriter mechanism this templating system is hidden from the end user; for example, a request for:

http://localhost/MyApplication/MyPage.aspx

is mapped under the hood to:

http://localhost/MyApplication/Template.aspx?UserControl=Controls/MyPage.ascx

All well and good - I had the basic URL rewriting mechanism in place in a couple of hours, and the start of a working template mechanism a couple of hours after that. However, then the problems started cropping up.

The first major issue cropped up with postbacks. In short, they didn't work. The reason for this is that the ASP.NET framework fills in the action attribute with a canonicalized path to CurrentExecutionFilePath, which is not what's wanted here.

Easy then - change the action in the template page. Wrong; whichever brain dead idiot designed the System.Web.UI.HtmlControls.HtmlForm class hid the implementation of the action attribute. So, you've got to jump through hoops to change the functionality.

My eventual solution was to subclass the HtmlForm and replace the instance in my template page with my new class, ActionForm. ActionForm overrides the RenderAttributes function, which creates a new HtmlTextWriter, my own subclassed ActionFormHtmlTextWriter. This class in turn overrides WriteAttribute which checks to see what attribute it's writing out - if it's "action", then the value is changed to the originally requested Url. Control is then passed back to the original HtmlTextWriter to write the value out. Anyway, if it gets someone else out of the mire, then great.

public class ActionForm : HtmlForm
{
    protected override void RenderAttributes(HtmlTextWriter writer)
    {
        string action = (string)Context.Items["VirtualUrl"];

        if (action == null)
        base.RenderAttributes(writer);

        using (ActionFormHtmlTextWriter virtualWriter = new ActionFormHtmlTextWriter(writer))
        {
            virtualWriter.ActionUrl = action;
            base.RenderAttributes(virtualWriter);
        }
    }

    private class ActionFormHtmlTextWriter : HtmlTextWriter
    {
        private string actionUrl;

        public ActionFormHtmlTextWriter(HtmlTextWriter writer) : base(writer)
        {
        }

        public ActionFormHtmlTextWriter(HtmlTextWriter writer, string tabString) : base(writer, tabString)
        {
        }

        public string ActionUrl
        {
            get { return actionUrl; }
            set { actionUrl = value; }
        }

        public override void WriteAttribute(string name, string value, bool fEncode)
        {
            if (value != null && String.Compare(name, "action", true) == 0)
                value = ActionUrl;

            HtmlTextWriter writer = (HtmlTextWriter)InnerWriter;
            writer.WriteAttribute(name, value, fEncode);
        }
    }
}


 

 Friday, September 24, 2004

Gig Cancelled

I've just had a call from Drummer Bloke to say that The Swan in Southampton have cancelled the gig we were booked for tonight; not that I can blame them - we've buggered them around a few times over the last year, so I'm not entirely suprised. Still, it's a shame.

 Friday, July 30, 2004

New Bloodbikes Site Live

I've finally got around to updating the Yeovil Freewheelers site using DotNetNuke; I should have done it much earlier, but Real Life™ kept on getting in the way....

Anyway, they're a worthwhile charity that deserve support.

 Thursday, July 22, 2004

Doom 3 Graphics Card Benchmarks

More Doom 3 news; (yes, I know it's sad to be this excited over a game, but I'm not the only one...).

id Software have generated some official Doom 3 benchmarks which show framerate data for high end graphics cards. But for me, that's not the really interesting bit; this quote is:

As of this afternoon we were playing DOOM 3 on a 1.5GHz Pentium 4 box with a GeForce 4 MX440 video card and having a surprisingly good gaming experience. Even a subtle jump to an AMD 2500+ with a GeForce 3 video card that is two years old will deliver a solid gaming experience that will let you enjoy the game the way id Software designed it to be. That fact alone should let many of you know that you will not be left behind in experiencing DOOM 3.

This just sounds too good to be true! So, am I going to be able to stick with my GeForce FX 5700 Ultra for the time being? Much as I'd love to go out and get a 6800 Ultra, finances just won't stretch to that at the moment.

 Monday, July 19, 2004

Doom 'System Shock' 3?

Many thanks to Andrew; he found scans of a PC Gamer review of the nearly-here Doom 3. The review: page 34, page 35, page 38, page 39, page 42, page 44. (Warning, there are some potential spoilers in the review, and in the rest of this entry).

What I found interesting was this section from the review:

As you make your way through the different levels of the base, the plot is revealed via the PDAs you pick up, and in brief conversations with the few NPCs who weren’t “turned” by the satanic attack. To make your way through the inevitably sealed-off access doors between levels, you’ll have to read through email that progressively reveals a conspiracy of apocalyptic proportions — the nefarious scheme of psychotic Dr. Betruger, UAC’s chief scientist who’s perverted a teleportation experiment to open up a portal into a hell-like dimension. (Oh, and as if you couldn’t guess, Dr. Betroger is also keen on transporting his hellion army to Earth.)

Reading that reminded me of the venerable System Shock 2 (probably one of the creepiest games ever made) - which is no bad thing in my book. Oh, roll on August!