Cambia Research - Supporting the Microsoft .NET Developer Community Supporting the Microsoft .NET Developer Community  

     | Home  | Articles  | Categories  | Coders  | Search  | Submit  | Contact Us    
The trouble with the world is that the stupid are cocksure and the intelligent full of doubt. --Bertrand Russell

Share Your Knowledge! -- Create and submit your articles the easy way with WebWriter.

Updated:10:21 PM CT Feb 13, 2007
Posted:10:20 PM CT Feb 13, 2007

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

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

Author: Steve Lautenschlager

Has DownloadsHas ImagesSnippetC#.NET 1.1ASP.NET
    Prev     1    2     Next  

 Downloads

PathHelperClass - 33.6 KB - ASP.NET 1.1 Solution containing PathHelper Class.
ASP.NET 1.1 DLL - 5.0 KB - A compiled version of the PathHelper assembly

 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.
    Prev     1    2     Next  

Add New Comment
CR Comments by Cambia Research
advertisement
 
Steve Lautenschlager (steve)
Steve is the founder and creator of Cambia Research. Developing and maintaining the site combines his passions for technology, writing and education.
Steve holds a Ph.D. in particle physics from Duke University, has worked at CERN, the European center for particle physics (where the web was born) and in Microsoft's web division with microsoft.com, msnbc.com and other web properties. Steve is a web consultant specializing in Microsoft.NET technologies. Read more here.


 
Copyright © Cambia Research 2002-2007. All Rights Reserved. steve [ at ] cambiaresearch.com