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.

+2


a source to share


5 answers


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)

0


a source


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?

+2


a source


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

+2


a source


Simply put, no. Why don't the files exist? Can you at least have empty files?

+1


a source


What you can do is something like this:

  • Use Scripting.FileSystemObject to detect the presence of files
  • Use Server.Exeecute to "include" files, or at least execute code.

The only problem is that files cannot use normal program scope variables.

0


a source







All Articles