SingingEels : Development Community & Resource

Login

Articles

  • ADO.NET (2)
  • ASP.NET (31)
  • LINQ (4)
  • Security (2)
  • Silverlight (2)
  • SQL (7)
  • Standards (5)
  • WCF (1)

Syndication

  • Articles RSS
  • Blogs RSS

Contribute

  • Our Authors List
  • Member Sign-Up
  • Suggestions Box

URL Re-Writing The Right Way! (It's Easy)

(Sep 14 2007 - 12:05:14 AM by Timothy Khouri) - [print blog post]

Recently (actually about 4 hours ago) I wrote a blog post about how to fake URL re-writing in ASP.NET. In fact, I even used that method on Eels for a long time as a very lazy work around until I decided how I wanted the structure of things to work. So, after writing that post, I realized the time had come and gone for allowing sloppy code, and that now I had to fix it.

So, I wanted to recap what I did in order to map your own HTTP requests, and how I've now fixed my bad ways (and how easy it was to do so). So, first of all, what I was doing before was tapping into the "Application_Error" method in the Global.asax file. If an exception was thrown in my web app, and there was no "Handler" assigned (which happens in a 404 scenario), I would then check the URL and if it was an attempt to read an article, I would redirect them to the right page myself.

void Application_Error(object sender, EventArgs e)
{
   // Hmmm, an error you say... that's funny, we don't even have

   // a page yet! This tells me that it's probably a "404" error

   // because the file they requested can't be found.

   if (this.Context.Handler == null)
   {
       // Hey ASP.NET, where *WOULD* the file be if one existed?

       string physicalPath = this .Request.PhysicalPath.ToLower();

       // Hmmm... that file *WOULD* be in my "Articles" directory!

       if (physicalPath.StartsWith(global_asax .articlesDirectoryPath))
       {
           // Don't worry lil-ASP.NET, I'll vouch for that request...

           // and I'll point it to the only real page in my "Articles" directory.

           this.Context.Handler = PageParser .GetCompiledPageInstance("~/Articles/Default.aspx",
                Server.MapPath("~/Articles/Default.aspx"), HttpContext.Current);

           // Oh yeah, and ignore that lil 404 error, would ya?

           this.Server.ClearError();
       }
   }
}

This was a very poor design and was supposed to only be a work around until I decided how I wanted the structure of Eels to be. Well, months have passed and now I've fixed the above to do what I should have done in the first place - make my own HttpHandlers. So here is what I added to my web.config:

<httpHandlers>
   <add verb="*"
       path="Articles/*.aspx"
       type="EelsArticleHandler" />
</httpHandlers>

And here is my HttpHandler class (which I have in my App_Code directory of course):

using System;
using System.Web;
using System.Web.UI;

public class EelsArticleHandler : IHttpHandler
{
   public bool IsReusable
   {
       get
       {
           return false;
       }
   }

   public void ProcessRequest(HttpContext context)
   {
       context.Handler = PageParser.GetCompiledPageInstance("~/Articles/Default.aspx", context.Server.MapPath("~/Articles/Default.aspx"), context);

       context.Handler.ProcessRequest(context);
   }
}

I guess doing things the right way isn't that hard now is it :)

  • Oct 17 2007 - 01:55:27 PM n3olee

    Hi Timothy.. Its Lee (You helped me via email with this URL rewriting)

    Just wanted to say a big thanks as I now have it working with my own HttpHandler Class and its awesome!! I am using ASP.NET VB and it took me a little bit of messing around to write it but thought I would post up the code for the custom class I used to help anyone else not using C# (Hope this is ok?)

    Imports System

    Imports System.Web

    Imports System.Web.UI

    Public Class TheArticleHandler

    Implements IHttpHandler

    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable

    Get

    Return False

    End Get

    End Property

    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

    context.Handler = PageParser.GetCompiledPageInstance("~/Articles/Default.aspx", context.Server.MapPath("~/Articles/Default.aspx"), context)

    context.Handler.ProcessRequest(context)

    End Sub

    End Class

  • Mar 13 2008 - 09:04:15 AM n3olee

    Hey Timothy..

    I think I have just stumbled on a problem with this, I have posted on the forums

    http://forums.asp.net/t/1233132.aspx

    Was wondering if you could let me know if you think the problem is down to this or not?

    Thanks

You must be logged in to add comments. If you have not already done so, you can create an account here. If you already are a member, you first need to login before you can comment.

Developer / Architect / Author

Blog Archives

  • August 2008 - (3)
  • July 2008 - (1)
  • June 2008 - (3)
  • May 2008 - (2)
  • April 2008 - (2)
  • March 2008 - (4)
  • February 2008 - (2)
  • December 2007 - (2)
  • November 2007 - (1)
  • October 2007 - (4)
  • September 2007 - (9)
  • August 2007 - (7)

Related Ads

SingingEels.com as of Aug 20 2008 - 08:58:16 PM - (0.0625)