ASP Lecture Series (14) Contains Files
Author:Eve Cole
Update Time:2009-05-30 19:58:52
The SSI directive provides users with a way to insert the contents of one file into another file before processing by the Web server. ASP only uses the #include directive for this mechanism. To insert a file into an .asp file, use the following syntax:
<!--#include virtual | file ="filename"-->
The virtual and file keywords indicate the type of path used to include the file, and filename is the path and name of the file you want to include.
Included files do not require a specific file extension; however, it is good programming practice to give included files an .inc extension to distinguish them from other types of files.
Using the Virtual keyword Use the Virtual keyword to indicate that the path begins in a virtual directory. For example, if a file named Footer.inc belongs to a virtual directory called /Myapp, the following line will insert the contents of Footer.inc into the file containing that line:
<!--#include virtual ="/myapp/footer.inc"-->
Using the File Keyword Use the file keyword to indicate the use of relative paths. Relative paths start from the directory containing the include file. For example, if your files are in the directory Myapp and the file Header1.inc is in MyappHeaders, the following line will insert Header1.inc into your file:
<!--#include file ="headers/header1.inc"-->
Note that the path to the included file Headers/header1.inc is relative to the including file; if the script containing the #include statement is not in the /Myapp directory, the statement will have no effect.
If the "Enable upper directory" option in Internet Services Manager is selected, you can also use the file keyword and ../ syntax to include files in the parent directory, that is, the upper directory.
Location of Included Files The included files can be in a directory within your Web site or outside of your Web site. Typically, you should have the included files located in the directory of your Web site. If an included file is located within your Web site, changes to the included file will be displayed the next time the browser requests the included file. However, if the included file is located outside your Web site, the change will not be reflected until the ASP application is restarted or the Web server is restarted. ASP detects changes to any included files in the application namespace (under the application's home directory).
Included files: Tips and warnings An included file can also include other files. An .asp file can include the same file multiple times if #include directives do not cause loops. For example, if the file First.asp contains the file Second.inc, Second.inc must not contain First.asp. A file cannot contain itself. ASP detects such looping or nested errors and, when detected, generates an error message and stops processing the requested .asp file.
ASP includes the file before executing the script command. Therefore, you cannot use script commands to create the names of included files. For example, the following script will not open Header1.inc because ASP executes the #include directive before it assigns a file name to the variable name.
<!-- This script will fail -->
<% name=(header1 & ".inc") %>
<!--#include file="<%= name %>"-->
Script commands and procedures must be entirely enclosed within script delimiters <% and %>, HTML tags <SCRIPT> and </SCRIPT>, or HTML tags <OBJECT> and </OBJECT>. That is, you cannot open a script delimiter in an .asp include file and then close it in an included file; the script or script command must be a complete unit. For example, the following script will not run:
<!-- This script will fail -->
<%
For i = 1 To n
statements in main file
<!--#include file="header1.inc" -->
Next
%>
The following script will run normally:
<%
For i = 1 to n
statements in main file
%>
<!--#include file="header1.inc" -->
<% Next %>