Skip to page content or Skip to Accesskey List.

Work

Main Page Content

Including Files In Asp

Rated 4.21 (Ratings: 7)

Want more?

  • More articles in Code
 
Picture of aardvark

Adrian Roselli

Member info

User since: 14 Dec 1998

Articles written: 85

As more people use Active Server Pages, I've noticed a trend that newer users can't figure out how to include files into their scripts. Part of the problem is that they expect an include directive to exist in the '<%' and '%>' script tags, but this isn't the case with Active Server Pages. Instead, you'll notice the include directive is reminiscent of Apache, or other web servers that allow server-side includes. As such, there are some rules to including files and keeping them from breaking your script that you have to keep in mind. So let's jump right in...

To include files in ASP pages:

<!--#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, MS docs will recommend giving included files an .inc extension to distinguish them from other types of files. I prefer to create a directory that contains all the files, and give them all an .asp extension. If someone finds that directory and/or file name (which can happen if your ASP scripts break and it shows the offending code), then the ASP parser will execute the script within. By default, the server will treat an .inc file as plain text and serve it as such. Often my include files are functions or navigation, so the result is a blank screen or an odd HTML page if I use an .asp extension. This prevents a user from seeing my code (which may include database connection information), and while it may generate an error, this is preferrable to showing all my script as plain text.

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 '/footer,' the following line would insert the contents of 'footer.inc' into the script containing the line:

  <!--#include virtual ="/footer/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 'headers', and the file 'header.inc' is in 'root\headers', the following line would insert 'header.inc' into your script:

  <!--#include file ="headers/header.inc"-->

Note that the path to the included file, 'headers/header.inc,' is relative to the including file; if the script containing this #include statement is not in the directory '/root', 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.

Other Notes:

  • You can have one include file include another. A file cannot include the file including it, nor can it include itself. That would be bad.
  • The include file must not have open control structures, nor can it be used to close control structures. You can't start an 'if' statement in your main page and have the corresponding 'end if' in the included file. The include file has to be able to stand on its own.
  • The ASP parser includes a file before executing any script commands. This means you can't put a variable in the filename and include files dynamically based on that variable's value. You can, however, load the include into a SUB and call the SUB only if certain criteria are met. For instance:

    <% IF foo = "bar" THEN %>

    <!--#include file ="include/bar.inc"-->

    <% ELSE %>

    <!--#include file ="include/foo.inc"-->

    <% END IF %>

    Will still load both include files into the ASP script, as opposed to only loading one of them. However, a cleaner way to do this might be:

    <% IF foo = "bar" THEN

    BAR

    ELSE

    FOO

    END IF %>

    <% SUB FOO %>

    <!--#include file ="include/foo.inc"-->

    <% END SUB %>

    <% SUB BAR %>

    <!--#include file ="include/bar.inc"-->

    <% END SUB %>

  • You must close your script tags before including a file. For instance:

    <% SUB FOOTER %>

    <!--#include file ="footers/footer.inc"-->

    <% END SUB %>

    Will work. However,

    <% SUB FOOTER

    <!--#include file ="footers/footer.inc"-->

    END SUB %>

    Will not.

A founder of evolt.org, Adrian Roselli (aardvark) is the Senior Usability Engineer at Algonquin Studios, located in Buffalo, New York.

Adrian has years of experience in graphic design, web design and multimedia design, as well as extensive experience in internet commerce and interface design and usability. He has been developing for the World Wide Web since its inception, and working the design field since 1993. Adrian is a founding member, board member, and writer to evolt.org. In addition, Adrian sits on the Digital Media Advisory Committee for a local SUNY college and a local private college, as well as the board for a local charter school.

You can see his brand-spanking-new blog at http://blog.adrianroselli.com/ as well as his new web site to promote his writing and speaking at AdrianRoselli.com

Adrian authored the usability case study for evolt.org in Usability: The Site Speaks for Itself, published by glasshaus. He has written three chapters for the book Professional Web Graphics for Non Designers, also published by glasshaus. Adrian also managed to get a couple chapters written (and published) for The Web Professional's Handbook before glasshaus went under. They were really quite good. You should have bought more of the books.

The access keys for this page are: ALT (Control on a Mac) plus:

evolt.org Evolt.org is an all-volunteer resource for web developers made up of a discussion list, a browser archive, and member-submitted articles. This article is the property of its author, please do not redistribute or use elsewhere without checking with the author.