When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sections:
Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
XML Info
Information:
Feedback
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback
Print this page.
Published: Wednesday, September 22, 1999

A "Print the Source Code" Script


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.

- continued -

'

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:

<A HREF="/scripts/showScript.asp?URL=<%=Request.ServerVariables("URL")%>">View the ASP code!</A>

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 strURL:

Dim strURL strURL = Request.QueryString("URL")

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: http://www.myserver.com/blah/myPage.asp?Name=Scott, 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 FileSystemObject (or FSO). However, 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.

Dim strDir, strFileName strDir = Request.ServerVariables("APPL_PHYSICAL_PATH") Response.Write strDir strFileName = Replace(strURL,"/","\") strFileName = strDir & strFileName

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!

Const ForReading = 1 Dim objFSO, objTextStream Set objFSO = Server.CreateObject("Scripting.FileSystemObject") Set objTextStream = objFSO.OpenTextFile(strFileName, ForReading) Response.Write "<HTML><BODY>" Response.Write "<XMP>" & objTextStream.ReadAll & "" Response.Write "" objTextStream.Close Set objTextStream = Nothing Set objFSO = Nothing

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 Request.ServerVariables Collection.

Happy Programming!


Attachments:

  • Source code for showScript.asp in text format



  • ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article