Conditional includes in Classic ASP - where the file may not exist on the server
I am currently in a situation where I have to make some additions to an application written in classic ASP using server-side JScript on IIS.
The additions that I need to make involve adding a series of includes to the server-side code to extend the application's capabilities. However, the inc files may not exist on the server in all cases, so I need the application to fall back to the existing behavior (ignore the includes) if the files do not exist, rather than generating an error.
I know that this can't be accomplished using if statements in the JScript code because of the way that SSI works, an开发者_高级运维d have not come across any ways of dynamically including the code on the server side, where the files may not exist.
Does anyone know of a way to accomplish this in classic ASP? Any help would be much appreciated.
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 one starting with "execute") is equivalent to an "include" directive, with the difference that it can be included inside an "if" statement (dynamic include).
Bye
If you are really brave, you can read the contents of the file and then Eval() it. But you will have not real indication of line numbers if anything goes wrong in the included code.
As a potentially better alternative: Can you not create some sanity check code in global.asa to create the include files as blanks if they do not exist?
Put simply, no. Why would the files not exist? Can you not at least have empty files present?
What you could do is something like this:
- Use Scripting.FileSystemObject to detect the presence of the files
- Use Server.Exeecute to "include" the files, or at least execute the code.
The only problem is that the files cannot share normal program scope variables.
The solution to this turned out to be to use thomask's suggestion to include the files and to set a session variable with a reference to "me" as per http://www.aspmessageboard.com/showthread.php?t=229532 to allow me to have access to the regular program scope variables.
(I've registered because of this, but can't seem to associate my registered account with my unregistered account)
精彩评论