This question comes up again and again. In building my own web site, I've often found the need to compose path names to resources on my site. For example, the image at the top of each of my web pages is determined dynamically based on what directory you're reading from. So, if you're reading a page from tools or one of the sub-directories, the src attribute of the <img> tag is:

http://www.sellsbrothers.com/images/tg_tools.gif

(where “tg” stands for “top graphic”). At least, that's what you get when you surf to my public web site. However, when I'm testing my web site internally before publishing it, the URL for the image is very different

http://localhost/sb/images/tg_tools.gif

Notice that in test mode, my web site is just another vdir and not the default web. And therein lies a problem. I can't just slap my domain name on the front of every URL – I need each URL on my site to be relative to the current site I’m testing, otherwise I’ll follow a link from my test site to my live site and not be testing anymore.

Of course, the immediately obvious solution is to use relative path names instead of absolute path names. Unfortunately, that doesn’t work as nicely as you’d like, either. The problem is that I’m using a single web control on every page of my web site to give it a common look 'n' feel. The web control will come into existence at all levels of my site:

/
/tools
/tools/monikers

I can't just use a hard-coded relative link to get to the image from the control, because even the relative path needs to be adjusted dynamically:

/images/tg_tools.gif
../images/tg_tools.gif
../../images/tg_tools.gif

The HttpRequest class provides a very promising method called MapPath that sounds useful to solve this problem. Unfortunately, MapPath maps to the physical location on the hard drive, not to an URL:

What we're really after is another kind of relative than we’ve had so far. We know how to build absolute path names and URL names starting from some known root. We also know how to build relative path and URL names starting from where we are. We need something else. To abstract away the differences between web servers, we need to be able to construct absolute path and URL names relative to the logical root of the vdir. No matter where I am in the hierarchy of my web site, I need to be able to translate:

images/tg_tools.gif

into:

http://localhost/sb/images/tg_tools.gif

when in test mode and into:

http://www.sellsbrothers.com/images/tg_tools.gif

when in release mode, but not have to change my code to do it (I tried that once – wow what a lot of work). ASP.NET provides no build in classes or methods that will do this for you. Luckily, it provides the necessary building blocks.

WebPathHelper

The core of mapping as relative from a logical root is to first find the logical root. This can be done using the HttpRequest URL property and HttpRequest ApplicationPath property:

No matter where WebPathHelper.RootUrl is called from, it will use the current HttpRequest object as context to return the root of the web site as an URL, including any vdir name as needed, as shown here:

This allows me to call WebPathHelper.MapPathFromRoot(@"foo\foo.mdb") and get the correct path name, where it's on my test machine or whether it’s on whatever machine ORCSWeb is using to host my site.

Where Are We?

In exploring this problem, I discovered that there are three kinds of paths, where only two have come up explicitly in my previous experience. We’re all familiar with absolute and relative URL and file names and our operating systems and browsers can consume them w/o trouble. However, building web applications requires also building absolute URLs and files from URLs and files relative to a logical root. The WebPathHelper will construct paths relative to the logical vdir root, which fills in the functionality missing from .NET for dealing with only relative and absolutely paths. This lets me add features to my site that require URLs and file names, test them locally and have confidence that they’ll work exactly the same way on my production site.

How I Figured This Out

This one was a pain to figure out. I started by noticing the problem and attempting some bone-headed temporary solutions that I'd prefer not to list here (too embarrassing : ). The key came when I asked some of my ASP.NET expert friends and they agreed that there was no good way to do what I wanted built into ASP.NET itself. Once that was established, I started digging through the information that ASP.NET, particularly the Request object, provided, which is where the aspx pages in the sample came from. From that information, I pulled out what I needed to build the WebPathHelper class and then deployed it on my own site, fixed the bugs that can only be found in a real-world deployment.

How To Avoid This In The Future

I don't know that I could have avoided this problem. I had actual resources, both file and URL, to reference that were relative to the root of the vdir. It would've been better to dig into the problem for real in the first place instead of my silly bandaids, but one can't always do things the "right" way the first time. The key was to recognize that I can a recurring problem happening in all kinds of spots on my web site, figuring out that they were all the same problem and sitting down to fix it once and for all.

Feedback Responses

Jamie Cansdale pointed out that the ~ character can be used in place of Request.ApplicationPath in some limited cases, e.g.