Enter the Tilde (~)
Browsing the asp.net forums I notice a lot of people putting the '~' (tilde) character on every control that has some sort src or href. Many think that the tilde character is a regular character that deals with file locations. Much like: /, ../, ../../ etc.
However, this little angle is actually an ASP.NET character that is a shortcut for" HttpRuntime.AppDomainAppVirtualPath. (say that 10x in a row). This property refers to the virtual application root - not the root of the web server. Remember that a Virtual Directory is in fact an Application (Server 2000 / XP). You'll notice that in Vista that the IIS team has gone the better rout and actually calls them "Applications" while still having the ability to have a virtual directory that functions as a directory.
So how do we use this character?
here are some examples.
If my Folder structure is as follow:
I have a master page within my main website, and content pages in my Admin. One thing newer web developers will do is declare an image like:
<img src="Images/MyImage.png" alt="my Image" />
That works for everything in the root directory, however once you get into the Admin directory - all images are broken. A way to resolve this is:
<img src="/Images/MyImage.png" alt="my Image" />
This can also lead into issues if you have a more complex structure. A quick way to resolve this is:
<asp:image id="myImage" runat="server" ImageUrl="~/Images/MyImage.png" AlternateText="My Image" />
This will always go to the root of your application and then to the Images directory. This is very useful with Hyperlinks when you are working with multiple directories.
Enter the ALT-SHIFT (Really... ALT-SHIFT)
I've heard about this but I've always just stored this away in the back of my mind. This little button combo allows you to select VERTICALLY in Visual Studio.
So, besides the nice little vertical line, what are the uses of this?
- Easy way to remove a single line of characters (say Line numbers from an online post) without having to format a bunch of things
- Easy way to remove comments if you commented out lines (You can use the shortcuts: CTRL E+C to commend and CTRL E+U to uncommented)
- Quickly change values or remove values in XML.
- Many many more.
Needless to say it's a beautiful tool that exists in Visual Studio (not just ASP.NET).