ASP Include Files

Server-side includes let you insert the full contents of one file into another ASP page at request time, using the #include directive, so shared pieces of markup or code can live in a single file instead of being copied onto every page.

Why Use Include Files?

Most sites repeat the same header, navigation menu, and footer on every page. Without includes, that markup would have to be pasted into every single .asp file, and updating the navigation would mean editing every page one by one. An include file lets you write that shared markup once and pull it into every page that needs it.

The #include Directive

A server-side include is written as a special HTML-style comment containing #include, followed by either the file or virtual keyword and a path. It is processed by the web server before any of the page's ASP script runs, and the included content is dropped in at exactly that spot. The file keyword uses a path relative to the folder the current page sits in.

Including a File in the Same Folder

<!--#include file="header.asp"-->
<html>
<body>

  <p>This is the main content of the page.</p>

<!--#include file="footer.asp"-->
</body>
</html>

Using virtual Instead of file

The virtual keyword uses a path that starts from the web application's root, regardless of which folder the current page lives in. This makes virtual the safer choice for shared files, since the same include line works correctly whether the calling page sits in the site root or three folders deep.

Including a File by Virtual Path

<!--#include virtual="/includes/header.asp"-->
<!--#include virtual="/includes/footer.asp"-->
KeywordPath Is Relative ToBest Used For
fileThe folder of the current pageIncludes that always sit next to the pages using them
virtualThe root of the web applicationShared includes called from pages at different folder depths
  • Reusable page pieces, like a shared header, footer, or sidebar navigation menu.
  • Common constants or configuration values, such as a database connection string.
  • Shared VBScript functions used by many different pages.
  • A standard set of error-handling helpers used across the whole site.
Note: Include files are commonly saved with an .asp extension, but some developers use .inc as a visual signal that the file is a partial, not a full page. IIS does not process .inc files as script by default, so if one is ever requested directly by a browser its raw code could be exposed — using .asp for every include avoids that risk entirely.
Note: The #include directive is resolved before your VBScript runs, so you cannot build the file path dynamically with a variable, and you cannot wrap an #include inside an If statement to include it conditionally — the include always happens, unconditionally, at the exact spot it appears in the file.

Exercise: ASP Include Files

What is the main purpose of using an include file in classic ASP?