[link] Rewriting the URL using IHttpHandlerFactory - Jon Galloway

[link] Rewriting the URL using IHttpHandlerFactory

Don Good told me about Jeff Putz's cool article on Rewriting the URL using IHttpHandlerFactory. I don't know how I missed that one. It's a great technique that gets around all the grimy hacks you need if you're doing this the standard way, using HttpContext.Current.RewritePath(someFile). This one instead maps a custom handler for ASPX files to your class which implements IHttpHandlerFactory. Then in the code logic, you just load the code from the physical ASPX file instead of redirecting to it and screwing with your context:

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

public class MyPageFactory : IHttpHandlerFactory
{
   
public IHttpHandler GetHandler(HttpContext context, string requestType, 
      
string url, string pathTranslated)
   {
      context.Items["fileName"] = Path.GetFileNameWithoutExtension(url).ToLower();
      
return PageParser.GetCompiledPageInstance(url, 
         context.Server.MapPath("~/Content.aspx"), context);
   }

   
public void ReleaseHandler(IHttpHandler handler)
   {
   }
}

Read the article - it's short and well written. Don showed me a prototype that was using this, and it worked perfectly. Great idea!

Published Thursday, May 19, 2005 7:50 AM by Jon Galloway
Filed under:

Comments

# Re: [link] Rewriting the URL using IHttpHandlerFactory

Very timely...thanks for the link.

Thursday, May 19, 2005 6:16 AM by mearls@hotmail.com (Michael Earls)

# re: [link] Rewriting the URL using IHttpHandlerFactory

Monday, December 29, 2008 1:35 PM by nick_cazeld

# re: [link] Rewriting the URL using IHttpHandlerFactory

Friday, May 15, 2009 4:37 AM by nick_reltrr

Leave a Comment

(required) 
(required) 
(optional)
(required)