|
July 5th, 2001
In this article you will read about the use of the tilde character (~) to reference the root of an application.
(as of Beta 2)
Introduction
You're probably asking yourself--why on earth would I need a new character to reference the root if I already have the slash (/)? Well, the answer is, they're functionality is different. The tilde references the root of the virtual web as opposed to the root of the web server.
The world before the .NET tilde
The reason why I think the tilde belongs to the Great Features section is because I missed it so badly in ASP. Let me tell you why. As you know (otherwise you wouldn't be reading this) my column is hosted on the aspalliance.com server. I have my own virtual web: /remas. All images I'm using on my column (two pieces) are stored in the _img subfolder. In order to reference them, I can either use an absolute path (http://www.aspalliance.com/remas/_img/) or a relative one (../../_img). Both have their downfalls, however--esp. because most of the links on my column are created dynamically.
Server-side includes, Act I: Not elegant but viable
I'm using include files quite extensively. All common elements of this column are stored in include files and assembled once a page is requested. One of the includes contains functions for displaying the table of contents. These functions inspect all subdirectories of a given folder and display a corresponding image (folder or document). Now, no matter how deep in the file hierarchy, the same images should be used. Piece of cake, you'll say--just use an aboslute path, e.g. /remas/_img/folder.gif. Yes, that would work... but what happens if I move my column to some other site? As a matter of fact, I have a mirror of the column on my local machine. And the virtual path is /www/aspalliance/remas/. The absolute path from above won't work on my local machine.
What I did actually did to overcome the problem is, I stored all server-specific data in one include file. Among other things, the file contains the a variable holding virtual application root. Instead of hardcoding the absolute references, I simply use the variable. By restraining server-specific data to one singe file, I can easily copy all but one files of column from the local machine to the aspalliance.com server. If the virtual application root changes, I need only to change one line of code in the whole column. That's pretty convenient.
Another option would be to use relative references. The function displaying images could use Server.MapPath(".") to determine its current position in the file structure. Having that it could easily add an adequate number of ../'s. If memory server me well, some of my functions still use that approach.
Server-side includes, Act II: The real pain
So far we have established that there are ways, albeit not very elegant, to work around the problem of referencing files. Well, it's not always true. The tricks that work well for links, are useless for referencing server-side includes--simply because they can't be created dynamically, at least not straight-forward. And that is a big problem. Here's why.
Let's consider a simple scenario. We want to include the file _functions.asp into the file you're viewing now. The table below shows the relevant sections of directory tree:
- _functions.asp
-
| \\www.aspalliance.com |
| | |
|
\remas |
| |
| |
\_lib |
|
| | |
_functions.asp |
- default.asp
-
| \\www.aspalliance.com |
| | | |
|
\remas |
| | | |
| |
\ASP.NET |
| | |
| | |
\GreatFeatures |
| |
| | | |
\Tilde |
|
| | | | |
\default.asp |
What will the include directive look like? We basically have two options:
- Virtual reference
-
<!-- #include virtual="/remas/_lib/functions.asp" -->
The advantage of this approach is that the reference will work no matter where in the directory structure the referring file is. The disadvantage is that it only work if the column is placed in a virtual directory named /remas.
- Relative reference
-
<!-- #include file="..\..\..\_lib\functions.asp" -->
The advantage of that approach is, it will work no matter what virtual directory is used for the column. This disadvantage, however, is that you need to adjust the include directive whenever you move the referring file in the directory tree. And you can't use one single template for the whole site. You need one per directory-tree level.
What we really need is a way to include files that combines both advantages of the approaches described above and thus gets rid of the disadvantage... Ladies and Gentlemen let me introduce the .NET tilde.
.NET Tilde comes into picture
The tilde character is the solution to the problems described above. You can use it to reference the root of the virtual web as opposed to the root of the site. That's how ~/path differs from /path.
Let's use the scenario above and see how the tilde saves our day.
- Virtual reference
-
<!-- #include virtual="~/_lib/functions.asp" -->
As you can see we're not using the name of the virtual web (/remas). Therefore I could move the column as a whole around to different web servers and different virutal webs an the includes won't break. I could also move the individual referring files to different directory levels and the references would endure.
Isn't that a great feature?
|