Resolving relative URL's from code
By now most people will know about ResolveURL and use it whenever they need to reference a page that may be in a different folder. I still regularly see user conrols that reference images using plain paths though, and they only work when you use them from certain folders. Create a new folder and use the existing user controls in there and suddenly all your images are broken.
So, rule 1, use server controls instead of HTML controls and use the ~ syntax to create a root-centric path which will work everywhere. There are a million examples of this out there - here's a good one at MSDN.
You can also resolve these ~ paths using Page.ResolveURL from your code-behind, but a slightly more unusual question came up yesterday that warranted this post - how do you resolve a ~ url from a class where there is no access to your Page?
Well, there is access to the current page from a class, it's just hidden away as HttpContext.Current.Handler. Most of the time this is pointing to your page, so a simple cast will get you your ResolveURL:
string niceURL =
((Page)HttpContext.Current.Handler).ResolveURL("~/folder/image.gif");
In practice you might want to put some error checking in there, since HttpContext.Current can be null during Unit testing for example, and even if there won't always point to a page. YMMV.
In CS replace HttpContext.Current with CSContext.Current.Context.
In DD replace HttpContext.Current with Context