How to do Url Rewriting with just an HttpHandler (without the side-effects) – level 400
public class ProductHandler: IHttpHandler, IRequiresSessionState
{
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
context.Items[“originalQuerystring”] = context.Request.QueryString.ToString();
context.Items[“originalPathInfo”] = context.Request.PathInfo;
string productCode = {some code to derive your product code};
string page = “~/product.aspx”;
string queryString = “productCode=” + productCode;
foreach(string key in context.Request.QueryString.Keys)
{
if(key != “productCode“)
{
queryString += string.Format(“&{0}={1}”, key, context.Request.QueryString[key]);
}
}
context.RewritePath(context.Request.Path, string.Empty, queryString);
Page hand = (Page)PageParser.GetCompiledPageInstance(page, context.Server.MapPath(page), context);
// Listen for event to rewrite url back before the page renders.
hand.PreRenderComplete += new EventHandler(hand_PreRenderComplete);
hand.ProcessRequest(context);
}
void hand_PreRenderComplete(object sender, EventArgs e)
{
HttpContext.Current.RewritePath(HttpContext.Current.Request.Path,
HttpContext.Current.Items[“originalPathInfo”].ToString(),
HttpContext.Current.Items[“originalQuerystring”].ToString());
}
}
Interesting. Do you know of a .NET 1.1 (pre-PreRenderComplete) equivalent?
Just curious, why do you implement IRequiresSessionState? Is that just leftover code or is that needed when the target page uses Session?
Another implimentation uses a HTTPHandler that inherits from IHttpHandlerFactory.
public class PageFactory : IHttpHandlerFactory
{
public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
{
return PageParser.GetCompiledPageInstance(url, context.Server.MapPath(“~/actualPage.aspx”), context);
}
}
URL ReWriting occurs before any template parsing, and also avoids “side effects”.
Is there a difference in performance between the two, are there advantages of one over the other?
You can also use the PreRender event. The idea is that you trim off any querystrings you used for the request before the Page is rendered, so you don’t want to tank the querystrings before Page or control code is finished with them.
Don,
If you put your derived information in the Context.Items cache, then that would work just fine, but if you add those items to the querstring, which is very common, then you need a step to remove them from the querystring before rendering.
I like it, I like it a lot. Seems sneaky, but cool nonetheless.
Wonderful!
Great! i was searching for this solution . stumpled on your sample , which solved my problem like sweet!
i was really scratching my head why context.RewritePath does not work for me , it litterelly returned blank pages for me.
would like to know what’s wrong with context.RewritePath alone returns blank responses and why url rewrite needed before the page renders
thanks
i got a doubt regarding adding a http handler. In web.config where should i write
.
and one more question regarding default handler
does it saves a file which is sent by client as http request into the specified folder?