Guides

ASP, Server Side Includes

How to replace includes of type File with includes of type Virtual

On servers with a Windows Server operating system (so with an IIS web server), the includes directive and the MapPath method of the Server object in ASP scripts only use absolute virtual paths whose root (/) corresponds to the web root of the website.
If, however, the scripts use includes directives of type file or, in general, relative physical paths (starting with ../), it may be important to know how to transform these directives appropriately for the IIS web server.

In terms of virtual absolute URLs, if the website is, for example, nomedominio.it, the root of the website (indicated by the / character) is the address http://www.nomedominio.it, which means that the virtual absolute path to the root of the website is only the / character; by placing the / character at the beginning of each path, we can avoid repeating the text http://www.nomedominio.it in the URLs and the paths are much simpler. For example:

URL: http://www.nomedominio.it/connessioni/connessione.asp
virtual pathe: /connessioni/connessione.asp
includes: <!-- #include virtual="/connessioni/connessione.asp" -->
Server.MapPath: Server.MapPath("/connessioni/connessione.asp")>

URL: http://www.nomedominio.it/variabiliglobali.asp
virtual path: /variabiliglobali.asp
includes: <!-- #include virtual="/variabiliglobali.asp" -->
Server.MapPath: Server.MapPath("/variabiliglobali.asp")

URL: http://www.nomedominio.it/testi/cultura/dolcestilnovo.html
virtual path: /testi/cultura/dolcestilnovo.html
includes: <!-- #include virtual="/testi/cultura/dolcestilnovo.html" -->
Server.MapPath: Server.MapPath("/testi/cultura/dolcestilnovo.html")

In this way, the includes or Server.MapPath directives will remain unchanged wherever they are carried out on the website, so if you need to move an asp or html script that contains virtual includes directives, you will no longer need to change the includes path.

In general, thanks to the virtual absolute path, it will be possible to insert an includes directive into a script regardless of the location of this script.

Example 1

Includes directives are used to insert the same code in several pages of a website and make changes easier (because they are centralised in a single file). In this example, we are going to insert a file that contains the html header code (generally common to all pages) in any asp or shtml page on the website.

Suppose we want to include in the script http://www.nomedominio.it/index.asp the html code whose url is: http://www.nomedominio.it/layout/header.html.

If you use an includes directive of type file in the index.asp script, then:

<!-- #include file="layout/header.html" -->

Using a virtual includes directive instead, in the index.asp script we add:

<!-- #include virtual="/layout/header.html" -->

As can be seen, the only difference between the path used in the include virtual directive and that of the include file directive, is the presence of the "/" characte.

Let us proceed with the analysis.

Suppose we want to insert the contents of the file located at the url http://www.nomedominio.it/layout/header.html into the script http://www.nomedominio.it/guestbook/index.asp.

If a file type includes directive is used, this line must be added to the index.asp script:

<!-- #include file="../layout/header.html" -->

If, on the other hand, a virtual includes directive is used, this line must be added to the index.asp script:

<!-- #include virtual="/layout/header.html" -->

Example 2

This example clearly shows how the virtual includes directive remains unchanged regardless of the position of the script that uses it; on the other hand, the file includes directive, must be modified depending on the position of the script that uses it.

The same applies to Server.
Suppose we want to read the text file located at the url http://www.nomedominio.it/contenuti/listino.txt, using the vbscript Scripting.FileSystemObject.

This library needs the physical path of the file, which in ASP is obtained using the MapPath method of the Server object.

If you want to read the file http://www.nomedominio.it/contenuti/listino.txt from the script http://www.nomedominio.it/mostralistino.asp, the lines of code to be used are as follows:

relative path
percorsofile = Server.MapPath("contenuti/listino.txt")
Set oggettoFileSystem = Server.CreateObject("Scripting.FileSystemObject")
Set oggettoFile = oggettoFileSystem.OpenTextFile(percorsofile, 1)

virtual path
percorsofile = Server.MapPath("/contenuti/listino.txt")
Set oggettoFileSystem = Server.CreateObject("Scripting.FileSystemObject")
Set oggettoFile = oggettoFileSystem.OpenTextFile(percorsofile, 1)

The only difference between the two paths is the '/' character.

Consider the following case.

You want to read the file http://www.nomedominio.it/contenuti/listino.txt from the script http://www.nomedominio.it/gadget/italiano/mostralistino.asp.

The lines of code to be used are as follows:

relative path
percorsofile = Server.MapPath("../../contenuti/listino.txt")
Set oggettoFileSystem = Server.CreateObject("Scripting.FileSystemObject")
Set oggettoFile = oggettoFileSystem.OpenTextFile(percorsofile, 1)

virtual path
percorsofile = Server.MapPath("/contenuti/listino.txt")
Set oggettoFileSystem = Server.CreateObject("Scripting.FileSystemObject")
Set oggettoFile = oggettoFileSystem.OpenTextFile(percorsofile, 1)

As with the includes directives, the virtual path remains unchanged for Server.MapPath regardless of the location of the script that uses it. Specifying relative paths means that a new path has to be found every time a file is called from a different location, increasing the chance of making mistakes when writing code.