Scott Hanselman

Brainfart? or ASP.NET madness

November 19, '03 Comments [4] Posted in ASP.NET
Sponsored By

I need a function from ASP.NET that gives me, reliably and guaranteed, without a config file, the base virtual directory of the current website.  So if I'm in c:\inetpub\wwwroot\web (ne http://localhost/web) then I want “/web/.” 

But (here's the fart part) I can't seem to find a way that gives it to me INDEPENDANT of the current Context's path.  Meaning, if the VERY FIRST (app start) page hit on the site is http://localhost/web/balances/balances.aspx then I still need “/web/“ not “/web/balances/.“

This is the closest I've got:

return HttpRuntime.AppDomainAppVirtualPath + "/";

However, this appears to occasionally (read: flaky) return a different path like /web/images/ or /web/someotherpagedirectory. 

What am I missing?

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb
Wednesday, 19 November 2003 00:59:04 UTC
So you need the highest level directory of a web request? Or do you need the base directory of a web application?

For the former, you can do something with the Request.ServerVariables["PATH_INFO"] value, like splitting it on '/' and grabbind the first element.

The latter is easier, and you can use Page.ResolveUrl("~/").
Wednesday, 19 November 2003 01:15:27 UTC
The base dir of the web app can also be gotten from Request.ApplicationPath. Though as Kurt points out, that isn't necessarily the left-most directory in the URL.
Thursday, 20 November 2003 04:26:23 UTC
How about HttpRequest's ApplicationPath property?
Friday, 21 November 2003 22:57:12 UTC
I use

System.AppDomain.CurrentDomain.BaseDirectory

Because you don't have to add a ref to System.Web from other libraries.. I usually do, but using this lets you get away with not having to. No reliance on an HttpRequest object
Sean
Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.