Including Files

Server-side include directives give you a way to insert the content of another file into a file before the Web server processes it. ASP implements only the #include directive of 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 you are using to include the file, and filename is the path and file name of the file you want to include.

Included files do not require a special file name extension; however, it is considered 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 a path beginning with a virtual directory. For example, if a file named Footer.inc resides in a virtual directory named /Myapp, the following line would insert the contents of Footer.inc into the file containing the line:

<!--#include virtual ="/myapp/footer.inc"--> 

Using the File Keyword

Use the file keyword to indicate a relative path. A relative path begins with the directory that contains the including file. For example, if you have a file in the directory Myapp, and the file Header1.inc is in Myapp\Headers, the following line would insert Header1.inc in 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 this #include statement is not in the directory /Myapp, the statement would not work.

You can also use the file keyword with ../ syntax to include a file from a parent, or higher-level, directory if the Enable Parent Paths option is selected in Internet Service Manager.

Location of Included Files

Included files can reside within a directory in your Web site or outside of your Web site. Generally, you should keep included files within Web site directories. If an included file resides within a directory in your Web site, changes to the included file appear the next time a browser requests the including file. If, however, the included file resides outside your Web site, changes will not appear until the ASP application is restarted or until the Web server is restarted. ASP detects changes to any including file that is contained in the application name space (under an application starting point directory).

Including Files: Tips and Cautions

An included file can, in turn, include other files. An .asp file can also include the same file more than once, provided that the #include directives do not cause a loop. For example, if the file First.asp includes the file Second.inc, Second.inc must not in turn include First.asp. Nor can a file include itself. ASP detects such loop or nesting errors, generates an error message, and stops processing the requested .asp file.

ASP includes files before executing script commands. Therefore, you cannot use a script command to build the name of an included file. For example, the following script would not open the file Header1.inc because ASP attempts to execute the #include directive before it assigns a file name to the variable name.

<!-- This script will fail -->
<% name=(header1 & ".inc") %> 
<!--#include file="<%= name %>"-->

Scripts commands and procedures must be entirely contained within the script delimiters <% and %>, the HTML tags <SCRIPT> and </SCRIPT>, or the HTML tags <OBJECT> and </OBJECT>. That is, you cannot open a script delimiter in an including .asp file, then close the delimiter in an included file; the script or script command must be a complete unit. For example, the following script would not work:

<!-- This script will fail -->
<%
For i = 1 To n
  statements in main file
  <!--#include file="header1.inc" -->
Next
%> 

The following script would work:

<% 
For i = 1 to n
  statements in main file
%> 
<!--#include file="header1.inc" -->
<% Next %>