Tuesday, November 22, 2005 9:04:04 PM UTC :: Filed Under ASP.NET

I’m going to have to make reading Scott Guthrie’s blog part of my daily ritual because nearly every post is packed with useful information!  This article shows how to run a web application on your development PC as though it was running at the top-level (or “root”) web site, even if it’s configured to run in a virtual root:

How to Run a Root “/” Site with the VS/VWD 2005 Local Web Server

Why is this cool?  If you haven’t already figured it out, here’s my lame explanation:

When you configure a web site on a Windows 2003 Server using IIS 6, each site has its own root directory because IIS 6 on a web server allows for the creation of multiple web sites.  However, on a development machine running Windows XP Pro and IIS 5, you can only have one web site defined which means all other sites must be contained in virtual root folders below the main site.   The problem that this creates is that it becomes difficult to reference file paths to static files and folders.

For example, if your site contains an “images” folder and a “templates” folder (which contains your .ASCX user controls), you will likely want to reference images from within one of your user controls.  On a web server, you could reference an image path like this:

<img src=”/images/someimage.gif” alt="My Alt Tag" />

However, that won’t work on a development PC running IIS 5 with a site defined as a virtual root.  The path would then have to be:

<img src=”/myvirtualroot/images/someimage.gif” alt="My Alt Tag" />

Starting to see the problem here?  In the second example, all your images will break when you copy your files from your development PC to the live web server.   Using the “../” method to navigate to images used to work:

<img src=”../images/someimage.gif” alt="My Alt Tag" />

However, do to naughty people trying to run scripts through the address bar in the browser, IIS often won’t allow for the “../” notation.  One solution is to use the method ResolveURL along with the “~/” prefix:

<img src="<%# Control.ResolveUrl(“~/images/someimage.gif”) %>" alt="My Alt Tag" />

But that’s just plain ugly code!   Utilizing Scott’s trick, you can reference your file paths as though you were working on a live web server without having to do any ugly work-around. Schweet!

Navigation
On this page....
Search
Archives
<November 2005>
SunMonTueWedThuFriSat
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910
Categories
Contact me
Send mail to the author(s) Contact Todd M. Taylor