Display Your Code

Say you want to write an article about web code/technology. How do you put html/asp/asp.net/php/etc. code in an ASP page and just display it (e.g., view the source)? How do you tell the server you just want to display the code and not execute it? Here is a solution:

<%
 whichfile=server.mappath("yourfilename.ext")
 
Set fs = CreateObject("Scripting.FileSystemObject")
 Set thisfile = fs.OpenTextFile(whichfile, 1, False)
 
counter=0
 do while not thisfile.AtEndOfStream
 counter=counter+1
 thisline=thisfile.readline
 response.write server.htmlencode(thisline) & "<br>"
 loop
 
thisfile.Close
 set thisfile=nothing
 set fs=nothing
 %>


What happens here is:
  1. Use the File System Object to read a line of the ASP page.
  2. Check if we are at the end of the page
  3. If not use server.htmlencode to write that line to the browser


display code asp Server.MapPath graphics FileSystemObject fso


Back To Top
© 1998 - 2017 psacake.com | My3C's

Version 7.2 | Advertise on this site