Reusing pieces of code is like picking off sentences from other people's stories and trying to make a magazine article. --Bob Frankston

PathHelper - Convert File and Folder Paths and URLs in an ASP.NET Application

Absolute, virtual, physical and application relative paths and their conversion

Downloads

Summary

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.

Definitions

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
  • Virtual
  • Physical
  • Application

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:

  • http://www.cambiaresearch.com
  • http://www.cambiaresearch.com/c4/CategoryIndex.aspx
  • http://www.cambiaresearch.com/c4/

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:

  • /
  • /Default.aspx
  • /c4/CategoryIndex.aspx
  • /c4/
  • /c4/Categories.aspx?category=Article+Type%7cSnippet

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:\Inetpub\wwwroot
  • C:\Inetpub\wwwroot\myweb\myapp\Default.aspx
  • C:\Documents and Settings\steve\My Documents\My Pictures\black.png
  • \\oak\PublicFolders\Steve
  • \\oak\PublicFolders\Steve\Blue6.htm

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:

  • Categories.aspx?category=Article+Type%7cSnippet
  • ArticleIndex.aspx
  • styles/wscms.css

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:

Portable URLs
string url = PathHelper.AppPath + "folder/page.aspx";
The PathHelper.AppPath Method
/// <summary>
/// The virtual application path:
/// "/myapp/"
/// "/apps/myapp/"
/// "/"
/// </summary>
public static string AppPath
{
	get
	{
		// get the regular application path
		string path = HttpContext.Current.Request.ApplicationPath;

		// ensure a trailing slash...
		if (path == "")
			return "/";
		if (path[path.Length-1] != '/')
			path += "/";
		return 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);

Credits

Credit goes to Matt Bell for the initial version of this class.

 

Version: 6.0.20200920.1535