.NET Tip of The Day
Learn one new .NET trick every day
Web GTD system from the creators of DotNetTipOfTheDay.org          Login or Join

How to get virtual path of web application

Getting the virtual paths local to server is pretty straight forward most of the time; just use Page.ResolveURL("~/whatever"). But what if you're in the BLL part of your app? Seeing as the Page.ResolveURL is not a static method you would have to create a new instance of Page before you can reference it... not good enough.

In BLL use System.Web.HttpRuntime.AppDomainAppVirtualPath static property to get local virtual path of web application:

string url = HttpRuntime.AppDomainAppVirtualPath + "/whatever";

Note: if web application is in a server root folder AppDomainAppVirtualPath returns just "/". If web application is in a non-root folder it returns virtual path of the folder without "/" in the end.

8/1/2007
RSS .NET Tip of The Day
Subscribe to receive one tip from the .NET Tips and Tricks Community per day.
Previous Tips of The Day
The best of the .NET Tips & Tricks Community.
.NET Practitioners .NET Tips & Tricks Community
Every .NET practitioner has a trick up in their sleeve. This is the place to share it with other .NET people.
Submit a Tip
Discovered a new trick? Share it with others.
My Tips
Manage tips you authored.