I was working on a web page where I wanted to add a link to take the visitor to the default document of the folder this particular page is in. This page (e.g. page1.aspx) was in a folder (e.g. folder1). I actually knew the default document in this folder was index.aspx, and could have just set the link's HREF to "index.aspx", but wanted to make this a little more generic so it didn't matter what the default document was. Just to be clear, the location of the page I was putting this link on looked like:
http://www.example.com/folder1/page.aspx
Initially, I set the link's HREF to:
"../folder1/"
This worked well. It would navigate the visitor to:
http://www.example.com/folder1/
But then I needed to copy page.aspx to another folder (e.g. folder2). So the page was going to exist in both folder1 and folder2. Once I copied page.aspx to folder2, I could have manually edited the link to:
"../folder2/"
This didn't have a very generic feel to it and I would always need to remember to change the HREF if I needed to copy the page again to other folders. So I decided to make this link a server side HyperLink control and create some fairly simple .NET code that would determine what the current folder the page was in by parsing the URL and setting the link's HREF so it would take the visitor to the default document of the directory the page was in. Once the .NET code determined the folder the page was in, it ended up setting the HyperLink's NavigateUrl to something similar to:
HyperLink1.NavigateUrl = "~/folder1/";
- or even -
HyperLink1.NavigateUrl = "~/folder2/";
The worked well, but I was surprised when I looked at the HTML source to see what the HREF resolved to. It essentially looked like:
<a id="HyperLink1" href="./">Go to the root of this folder</a>
As you can see, a HREF of "./" is the default document or root of the current folder! Instead of running this .NET code I created to parse the URL and determine the current folder name, all that's needed is just statically setting the HREF to "./". This gave me flashbacks to the old DOS days where running a simple DIR command (in a directory other than root) always results in the first two lines being:
<DIR> .
<DIR> ..
The "." DIR refers to the current folder and the ".." DIR refers to the parent folder. You can still see this today by opening up a command prompt and running DIR.
It's also worth mentioning that you can use "./" when redirecting a visitor in server side code:
Response.Redirect("./");
I'm sure I'll now start finding lots of places to sprinkle "./" HREFs in!