PathHelper - Convert File and Folder Paths and URLs in an ASP.NET Application
Absolute, virtual, physical and application relative paths and their conversion
The ASP.NET developer is constantly needing to manage file, folder and webpage references in .NET. This can be a tedious task.
Page 1: First I will present a standard set of definitions so that we know what we mean when use words like "absolute path", "relative path", "physical path", "application path", etc.
Page 2: Then I will present some of the code to retrieve and manage this information.
Download: For the full PathHelper class which makes managing paths as easy as pie, download the sample solution above.
I will talk primarily about 4 types of paths:
- Absolute URL
- Virtual URL
- Physical Path
- Application Relative Path
I will get lazy sometimes and drop the 'URL' or 'Path' part and simply refer to them as
Absolute URL: The absolute URL is is the fully qualified URL beginning with the scheme (ie. http, ftp, file, etc.)
For more on the Anatomy of a URL click here.
So, some examples of absolute URLs are:
Virtual URL: A virtual URL begins with a slash "/" and represents the root of the website. It is everything that follows the domain. In URL parlance it is the path, query and fragment of a URI.
Virtual URLs are not relative URLs. Virtual URLs are based on the root of the website.
Some examples of virtual URLs are:
Physical Path: The term physical path describes a fully qualified Windows-type path name that begins with a drive letter or network address and points to a specific folder or file on a computer or network.
Some examples of physical paths:
- C:\Documents and Settings\steve\My Documents\My Pictures\black.png
Application Relative Path: Application path is a path that is relative to the current ASP.NET application folder. In ASP.NET 2.0 the tilda (~) was introduced to indicate an application relative path, but no such help exists in ASP.NET 1.1 and earlier.
Note that an application relative path can represent both a URL and a physical path by pointing to a folder or file. To have a valid URL or physical file one would need to convert the application path to the appropriate type using the PathHelper class I provide.
Some examples of application relative paths:
Okay, that's it for definitions. If you've worked with this stuff you know how important it is to know what you mean be certain words.
On the next page, I will show you some code for retrieving and converting some of these different path types.
An important concept in managing path conversions is the application path. An ASP.NET application resides in a folder at either the root of a website or a subfolder. In order to have a portable web application, you don't want to hard code URLs in the application. All URLs should be essentially application relative.
But this means we need to programmatically determine the application path. URLs should be coded as follows:
string url = PathHelper.AppPath + "folder/page.aspx";
The PathHelper.AppPath Method
/// The virtual application path:
public static string AppPath
// get the regular application path
string path = HttpContext.Current.Request.ApplicationPath;
// ensure a trailing slash...
if (path == "")
if (path[path.Length-1] != '/')
path += "/";
Using the PathHelper Class - Get path type
string path = TextBox1.Text.Trim();
// test whether path is a physical path
bool isPhysical = PathHelper.IsPhysicalPath(path);
// test whether path is an absolute URL path
bool isAbsolute = PathHelper.IsAbsolutePath(path);
// test whether path is a virtual URL path
bool isVirtual = PathHelper.IsVirtualPath(path);
// test whether path is application relative
bool isApplication = PathHelper.IsApplicationPath(path);
Credit goes to Matt Bell for the initial version of this class.