I am writing a simple HttpHandler for URL rewriting, but I’m hitting a brick wall.
I have created a HttpHandler class that’s really simple just to test things:
public class HttpHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.RewritePath("default.aspx", false);
//Rewriter.Rewrite(context);
}
public bool IsReusable
{
get
{
return true;
}
}
}
I then have the following verb in the web.config:
<httpHandlers>
<add verb="*" path="*" type="Tizma.CMS.Runtime.HttpHandler"/>
</httpHandlers>
I basically want all incoming URL’s to go through this rewritter. When I run this, ProcessRequest fires, but the RewritePath never gets to default.aspx.
Please bare in mind this is just a test and eventually default.aspx will be passed a query string along the lines of ?pageid=2 I just wanted to figure out how httphandlers worked first.
What am I doing wrong?
Andy – you can’t call RewritePath() in a handler – it’s way to late for that at that time. By the time you hit your handler the request has already routed to completion and RewritePath() doesn’t do anything.
RewritePath must be called very early in the request cycle (like BeginRequest or anything before the CacheModule kicks in) to be effective so you most likely need an HttpModule and hook the appropriate early pipeline event.