Have you ever wanted to be able to print out the source code of your ASP page? Perhaps you want to be
able to show people your ASP source code, or you want to provide a way to view your own ASP pages from
a remote machine. This article will examine how to accomplish this task using a couple
ServerVariables and the
FileSystemObject. Before I begin delving into the code, let's take a moment to examine the process I will be going through. I will be creating a script,
showScript.asp, that takes, as a parameter in the querystring, a URL to the web page whose code we want to display.
showScript.asp will then take that information and use the
FileSystemObject to open the web page in a
TextStream object. We will then dump the contents of the file to the browser, so that the code is shown.
For this article, we will assume that to display the source code of a given ASP page, you will provide
a simple hyperlink. This hyperlink is really quite simple, it just needs to point to the
showScript.asp page and pass the URL of current ASP page. You can access the current URL
of an executing ASP page by using
Request.ServerVariables("URL"). So, to create our hyperlink, would could simply use:
Now we are ready to write
showScript.asp. First, we need to determine the URL of the
page whose source code needs to be displayed. This URL was passed through the querystring as a variable named
URL. So, let's store this URL in a variable named
It is important to remember that
Request.ServerVariables("URL") does not return the entire URL of the current ASP script. Rather, it just returns the part after the http://www.servername.com, and before the querystring. So, if on our ASP page with the hyperlink to view the ASP code, the actual full URL is:
Request.ServerVariables("URL") would return:
/blah/myPage.asp. Therefore, the
URL parameter that
showScript.asp is receiving, does not contain the
http://www.myserver.com or the querystring.
Now that we have the URL, we need to open up the web page using the
FSO expects to have the address of the file as a physical address (i.e.
C:\InetPub\wwwroot\myASPScript.asp). We will resolve the URL by obtaining first the web application's root directory, and appending the
URL parameter to this root directory. The physical address of your web application's root directory is stored in the
APPL_PHYSICAL_PATH environment variable, which can be accessed with:
Request.ServerVariables("APPL_PHYSICAL_PATH"). We then want to replace all of the forward slashes in the
strURL variable with backward slashes. Finally, we want to append
strURL to the end of the root web's physical address. The following code will suffice.
When this code finishes executing,
strFileName will contain the full, physical address to the ASP page whose source code we wish to display. The last step is to use the
FileSystemObject to open the file and dump its contents to the screen. Here is code that will do just that!
The <XMP> tag ignores all HTML tags. It simply dumps the contents to the screen with ZERO formatting.
For a more detailed article on the
FileSystemObject, be sure to check out Reading/Writing Text Files using FSO. For more information on the
Request.ServerVariables collection, check out Using the
showScript.aspin text format