One of the more obscure - but potentially useful - methods in the portion of the .NET Framework that comprises ASP.NET is the HttpContext class's RewritePath method Used internally by ASP.NET to strip session IDs from URLs when cookieless session state is enabled,. RewritePath Can Also Be buy to "fake" URLS WITHIN An Application. To Demonstrate, Consider The Following Aspx File, Which We'll Call ReWritePath.aspx:
body>
html>
Void Page_Load (Object Sender, Eventargs E)
{
String ID = Request.QueryString ["ID"];
IF (id! = null && id! = string.empty) {
Switch (id) {
Case "1":
Output.text = "Give me chastity and"
"Continence, but not yet."
Break;
Case "2":
Output.text = "a programmer is a device"
For Turning Coffee Into Code. "
Break;
Case "3":
Output.text = "Bless is the man who,"
"Having Nothing to Say, Abstains from"
"GIVING WORDY Evidence of the Fact.";
Break;
}
}
}
script>
IF this page is deployed in a Virtual Directory named foo, The Following Urls Invoke The Page and Display Three Different Quotations:
http: //.../foo/rewritepath.aspx? id = 1
http: //.../foo/rewritepath.aspx? id = 2
http: //.../foo/rewritepath.aspx? id = 3
So far, so good. Now suppose That You'D Like Users to Be Aable To Display, "Phantom" URLS:
http: //.../foo/quotes/page1.aspx
http: //.../foo/quotes/page2.aspxhttp: //.../foo/quotes/page3.aspx
You can accomplish this little bit of magic - Converting a url of the form ... / quotes / page1.aspx INTO A URL of the form ... / shutritepath.aspx? Id = 1 on the fly - with httpContext.rewritePath. The trick is to grab each request for /quotes/page1.aspx, /quotes/page2.aspx, and so on as it entes ASP.NET'S HTTP PIPELINE AND CONVERT INTO a Request for /rewritepath.aspx?id=1, / REWRITEPATH.ASPX? ID = 2, And so on. Here's a global.asax file what does Just That:
Void Application_BeginRequest (Object Sender, Eventargs E)
{
//
// Todo: Convert a path of the form
// ... / quotes / page1.aspx Into a path of the form
// ... / rEWRITEPATH.ASPX? ID = 1
//
HttpContext context = httpContext.current;
String OldPath = context.request.path.tolower ();
String token = "/ quotes / page";
INT i = OldPath.indexof (Token);
INT LEN = token.length;
IF (i! = -1) {
INT j = OldPath.indexof (".aspx");
IF (j! = -1) {
String id =
Oldpath.Substring (i LEN, J - (i LEN));
String newpath =
OldPath.Replace (Token ID ".aspx",
"/rewritepath.aspx?id=" id);
Context.rewritePath (NewPath);
}
}
}
script>
Application_BeginRequest fires at the beginning of every request. This implementation extracts the path portion of the URL targeted by the request (for example, /foo/quotes/page1.aspx), replaces it with a path that refers to the real page (for example , /foo/rewrite.aspx?id=1), and calls RewritePath to retarget the request Try it:. Drop RewritePath.aspx into wwwroot on your Web server and try to invoke it by typing this URL into your browser's address bar: http : //localhost/quotes/page1.aspx
That Should Generate A "Page Not Found" Error. Now Copy Global.asax to wwwroot and try just fin, thanks to the indirection afforded by application_beginRequest.
RewritePath has many applications. You can use it, for example, to convert query strings into path names so pages that use query string parameters to drive content can be bookmarked in browsers. You can also use it to obfuscate path names within your application for security Reasons. Now That You Know RewritePath EXISTS, You May Find Other Uses for IT, TOO.