473,382 Members | 1,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Url Rewriting with just an HttpHandler (without the side-effects)

Jefrey Palermo

How to do Url Rewriting with just an HttpHandler (without the
side-effects) - level 400
posted on Wednesday, August 10, 2005 7:21 PM
If you are interested in doing Url Rewriting with an HttpHandler
instead of an HttpModule, then this is the example for you. Suppose
you are deriving a piece of information from Url, like a product code.
To process it, you have a page that accepts the product code as a
querystring variable. Look at the code below for how you can present a
friendly Url while abstracting that away from how you actually process
the request. You can do postbacks and everything, and the Url will
never revert back to the "ugly" Url.

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.Curren t.Request.Path,

HttpContext.Current.Items["originalPathInfo"].ToString(),

HttpContext.Current.Items["originalQuerystring"].ToString());

}

}

10 Comments

Nov 19 '05 #1
0 1389

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Sin Jeong-hun | last post by:
Hello. I created a very simple program which just pops up a MessageBox saying "Hello,world!". I compiled it and put it on my homepage and created a hyperlink to it. When I click the hyperlink to...
9
by: cody | last post by:
There should be a way to mark methods without sideeffects with a special Attribute so that the compiler can recognize them and is able to issue a warning: public class string { //...
5
by: cj | last post by:
Normally, a dotNET application will throw an exception when something goes wrong, and the exception can be caught easily and reported to the user. However, users have been reporting that our...
7
by: zeecanvas | last post by:
Hi, First of all: Yes, I know global variables are bad, but I've a huge amount of legacy code, and I've to maintain it _as_is_. I'm maintaining a big program. I moved all (program-wide scope)...
3
by: Lukasz | last post by:
Hello i got problem with vs 2005 std, and it is pissing me off. Im doing some job, for example i wanted to drag my own component into form, and second after that vs just closes without any info,...
7
by: AshokG | last post by:
Hi, If you use just throw without parameter should preserve the complete stack trace and the exception information. for example: 1. private void Bar() 2. { 3. try
2
by: sofeng | last post by:
I would like to use the following recipe to transpose a list of lists with different lengths. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/410687 Here is an example of what I would...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.