For most websites, the pages that make up the site all share a common layout and set of elements. ASP allows you include external files within ASP pages, by using the
#include ASP directive. This is very useful for creating common pieces of ASP code, which can be reused by many ASP pages within your ASP application.
For example, you may want to implement a common header or footer. If you were using plain HTML, you would have to copy that code on each page in your site. If you needed to make a change, you would need to open each file and update the pages.
#include directive, you can separate the header and footer into two different files, then reference each of the files from within each web page. If you need to make a change to the header or the footer section, you only have to update the two files.
You can insert the contents of one file such as a text, HTML, or even ASP file, into another ASP file, by using the ASP
#include directive. The web server inserts the external file prior to serving the HTML output back to the user’s browser.
To include a file in an ASP page, place the
#include directive inside of HTML comments tags.
<!-- #include file|virtual = "filename" -->
file keyword to indicate a relative path. Begin the relative path with the name of the directory that includes the file.
For example, if you have a file name called
header.inc and it is located in a folder called “includes”, then the following line would insert the contents of
<!-- #include file = "includes\header.inc" -->
virtual keyword to indicate a path beginning with a virtual directory. If a file named
header.inc is located in a virtual directory named “/includes”, then the following line would insert the contents of
<!-- #include virtual = "/includes/header.inc" -->
You should note that the files you are including do not require the
.inc extension. If you have sensitive information in your included file, you may want to configure the file as an ASP file. So that even if the file is accessed directly, the server-side code is not exposed back to the user as plain HTML.
In addition, note that included files are processed and inserted before the ASP scripts are executed. For example, the following code will not work as expected:
<% fileName="footer.inc" %> <!--#include file="<%fileName%>"-->