Any code of your own that you haven't looked at for six or more months might as well have been written by someone else. --Eagleson's Law
Welcome to my blog and project site for Microsoft.NET development.

I've been a full time .NET developer for ten years, but I didn't start my professional life as a programmer ... more
Share/Print this page:

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

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

By steve on February 13, 2007.
Updated on January 22, 2012.
Viewed 21,639 times (0 times today).
Article AttributesArticle AttributesArticle TypesLanguagesTechnologies
Has DownloadsHas ImagesSnippetC#ASP.NET

Downloads

Contents

Summary

Contents

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

Contents

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.

Back to Top

User Comments (0)

Post Your Comment
  You may post without logging in or login here.
Display Name: Required.
Email: Required. Will not be shown. Used for identicon.
Comment:
Allowed tags: <quote></quote>, <code></code>, <b></b>, <i></i>, <u></u>, <red></red>
 
   Please type text as shown in the image at left.