Conditional includes in classic ASP - where the file may not be present on the server
I am currently in a situation where I need to make some additions to an application written in classic ASP using server-side JScript on IIS.
The additions I need to make include adding a number of inclusions to the server-side code to extend the capabilities of the application. However, inc files may not exist on the server in all cases, so I need the application to fall back to existing behavior (ignore the included ones) if the files don't exist, rather than throwing an error.
I know this cannot be accomplished with if statements in JScript code because of the way SSI works and does not run into any way dynamically, including server side code where files may not exist.
Does anyone know how to do this in classic ASP? Any help would be much appreciated.
a source to share
The solution to this turned out to be using the thomask suggestion to include files and set a session variable with a link to "me" at http://www.aspmessageboard.com/showthread.php?t=229532 to allow me access to regular program scope variables.
(I signed up because of this, but can't seem to link my registered account to my unregistered account)
a source to share
If you are really brave, you can read the contents of the file and then Eval () it. But you won't have any real indication of line numbers if something goes wrong in the included code.
As a potentially better alternative: Can't you create health check code in global.asa to create included files as spaces if they don't exist?
a source to share
Here's a script to dynamically include asp files:
<%
' **** Dynamic ASP include v.2
function fixInclude(content)
out=""
if instr(content,"#include ")>0 then
response.write "Error: include directive not permitted!"
response.end
end if
content=replace(content,"<"&"%=","<"&"%response.write ")
pos1=instr(content,"<%")
pos2=instr(content,"%"& ">")
if pos1>0 then
before= mid(content,1,pos1-1)
before=replace(before,"""","""""")
before=replace(before,vbcrlf,""""&vbcrlf&"response.write vbcrlf&""")
before=vbcrlf & "response.write """ & before & """" &vbcrlf
middle= mid(content,pos1+2,(pos2-pos1-2))
after=mid(content,pos2+2,len(content))
out=before & middle & fixInclude(after)
else
content=replace(content,"""","""""")
content=replace(content,vbcrlf,""""&vbcrlf&"response.write vbcrlf&""")
out=vbcrlf & "response.write """ & content &""""
end if
fixInclude=out
end function
Function getMappedFileAsString(byVal strFilename)
Dim fso,td
Set fso = Server.CreateObject("Scripting.FilesystemObject")
Set ts = fso.OpenTextFile(Server.MapPath(strFilename), 1)
getMappedFileAsString = ts.ReadAll
ts.close
Set ts = nothing
Set fso = Nothing
End Function
execute (fixInclude(getMappedFileAsString("included.asp")))
%>
The last line (the first one starting with "execute") is equivalent to the "include" directive, with the difference that it can be included inside an if (dynamic include) statement.
Bye
a source to share