ASP.NET path discovery#
Sometimes the guys at Microsoft just strike me with amazement. This happens a lot but I'll limit myself to sharing this one with you - for now ;-)

I'm working on an ASP.NET project, I have a folder structure like /root/subdir where all my aspx pages reside. Now I learned from last time that it's better to keep my user controls (ascx) in a separate folder (/root/subdir/controls in this case) to keep them from cluttering my actual web's folder. Now inside one of those user controls I'm adding an that should point to "/root/subdir/images/help.png" - sounds easy but I don't want to use absolute url's since I want to be able to move the site into another root or rename the subdir or whatever.

My first guess was just to use "images/help.png" but apparently this becomes relative to the executing control's path, resulting in "/root/subdir/controls/images/help.png". No good. I could go for "../images/help.png" but I don't like this kind of path walking too much since it depends on my project's directory structure, not on my actual web path. Fortunately this doesn't wind up in the resulting html: it will become "images/help.png" and not "controls/../images/help.png" so that's ok.

I could use Request.ApplicationPath but that only gives me "/root" so I'd still have to append 'subdir' to it, and as I said I want to keep away from hardcoding paths in my application.

The logical thing to try next is to use the Request object since that contains the path of the executing aspx page - just what I need because it should be relative to this path. Request.Path contains the page name so I can't use that directly: it's something like /root/subdir/page.aspx and I just want the first part, the directory name. I was naive enough to hope that System.IO.Path.GetDirectoryName(Request.Path) would discover the path separator or allow me to override it easily but nope, I get "\root\subdir/images/help.png". That works in Internet Explorer but it's ugly and non-standard so there's no way I'm using that.

So I'm sniffing around in the QuickWatch window looking at the Request object and what do I see? Request.BaseDir gives me "/root/subdir" - exactly what I need! I punch it in my code but to my big surprise I find that the BaseDir property is declared private! Why? There must be some reason - at least I hope so - because I also noticed that Request.Url.BasePath points to the exact same thing, but it's also private. Again: why?! These things really blow my mind.

I guess I'll wind up doing "../images/help.png", I'm not in the mood to strip off the page and querystring from Request.Path myself. Certainly not since it's already been done by Microsoft but they decided not to share it.
Wednesday, November 08, 2006 4:25:58 PM (Romance Standard Time, UTC+01:00)
It is really strange that this quite usefull property is hidden to private. However, Request.ApplicationPath gives what you need.

Regards,
Oleg.
Wednesday, April 25, 2007 5:04:22 PM (Romance Standard Time, UTC+01:00)
Have you tried specifying Page.ResolveUrl from within the UserControl? Sounds to me like the following would do exactly what you need:

Page.ResolveUrl("~/AppRoot/subdir/images/help.png")
Friday, June 01, 2007 7:02:25 PM (Romance Standard Time, UTC+01:00)
Good one...

You can find more on asp.net website path @ http://dotnetguts.blogspot.com/2006/10/aspnet-website-paths.html

DotNetGuts (DNG)
http://dotnetguts.blogspot.com
Comments are closed.
All content © 2012, Jelle Druyts
On this page

Recent Photos
www.flickr.com
This is a Flickr badge showing public photos from Jelle Druyts. Make your own badge here.
Advertising
Top Picks
Statistics
Total Posts: 350
This Year: 0
This Month: 0
This Week: 0
Comments: 530
Archives
Sitemap
Disclaimer
This is my personal website, not my boss', not my mother's, and certainly not the pope's. My personal opinions may be irrelevant, inaccurate, boring or even plain wrong, I'm sorry if that makes you feel uncomfortable. But then again, you don't have to read them, I just hope you'll find something interesting here now and then. I'll certainly do my best. But if you don't like it, go read the pope's blog. I'm sure it's fascinating.

Powered by:
newtelligence dasBlog 2.0.7226.0

Sign In