Differences between root and subfolder applications Tuesday, June 03, 2008

IIS allows websites to be hosted in virtual folders that do not correspond to the on-disk organization. For example, if you publish a website located in C:\Websites\MyWebsite2\ on virtual folder /Web1/Test/, The URI to access it would be http://mycomputer/Web1/Test/.

The following table contains data from single web site being accessed from two different locations. One channel is the virtual folder /URLTests on port 80, and the other is the root website on port 87.

Property http://localhost:87/content/pages/ test.aspx/pathinfo?query=value http://localhost/UrlTests/content/pages/ test.aspx/pathinfo?query=value
Request. ApplicationPath / /UrlTests
Request. RawUrl /content/Pages/test.aspx/pathinfo?query=value /UrlTests/content/Pages/test.aspx/pathinfo?query=value
Request. AppRelativeCurrent ExecutionFilePath ~/content/Pages/test.aspx ~/content/Pages/test.aspx
Request.Current ExecutionFilePath /content/Pages/test.aspx /UrlTests/content/Pages/test.aspx
Request. FilePath /content/Pages/test.aspx /UrlTests/content/Pages/test.aspx
Request. Path /content/Pages/test.aspx/pathinfo /UrlTests/content/Pages/test.aspx/pathinfo
Request. PathInfo /pathinfo /pathinfo
Request. PhysicalApplicationPath C:\Users\Nathanael\Desktop\WebApplication2\ C:\Users\Nathanael\Desktop\WebApplication2\
Request. PhysicalPath C:\Users\Nathanael\Desktop\WebApplication2 \content\Pages\test.aspx C:\Users\Nathanael\Desktop\WebApplication2 \content\Pages\test.aspx
Request. Url.OriginalString http://localhost:87/content/Pages/ test.aspx/pathinfo?query=value http://localhost:80/UrlTests/content/Pages/ test.aspx/pathinfo?query=value

Note: CurrentExecutionFilePath (and the AppRelative version) both differ from FilePath with Server.Execute or Server.Transfer is used. CurrentExecutionFilePath changes to reflect the executing file, while FilePath remains unchanged. Spaces in the table above were inserted to allow wrapping of the text - the actual data contains no whitespace.

This should illustrate why application-relative paths (~/file.aspx) should be used wherever possible - they permit the site to be hosted in virtual folder as well as in a site root. Can you imaging the maintenance costs if you needed to move your site, and were using absolute paths, such as (/vdir/file.aspx)? You may not currently need to host the site on a virtual folder for dev purposes, but what about 5 years from now when you want to keep the old system online in a subfolder of the new site?