Home | Contact Us

CSharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript

ASP.NET PhysicalApplicationPath

This ASP.NET article shows how to use the PhysicalApplicationPath property.

Paths. An ASP.NET server has physical paths.

These are different from virtual paths. There are different ways of resolving paths, including the PhysicalApplicationPath property.

First, we use PhysicalApplicationPath and append physical paths to it. From MSDN: "It gets the physical file system path of the currently executing server application's root directory."

Info: In other words, PhysicalApplicationPath will return the location on the server that your web site is stored.

And: It won't return any URLs for the browser. URLs are not physical paths. We next look at some examples.

Example Request.PhysicalApplicationPath

C:\Users\Sam\Documents\Visual Studio 2008\WebSites\PerlsCom1\

To use PhysicalApplicationPath, you must have an HttpRequest handy. HttpRequest is part of the System.Web namespace. On an aspx page, you can access the PhysicalApplicationPath like this.

Also: The Request object can be accessed with "HttpContext.Current. Request.MapPath." It is sometimes best to store it in a variable.

Page that uses PhysicalApplicationPath: C#

public partial class Default2 : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
	// Example usage of PhysicalApplicationPath in Page.
	string path = Request.PhysicalApplicationPath;
	// Will write a result similar to the above table's example.

PhysicalApplicationPath is faster and serves a narrower purpose. MapPath can be used to resolve the root path, with MapPath("~/"). There are big performance differences here.


PhysicalApplicationPath usage: C#

// Find physical path with PhysicalApplicationPath and concat.
for (int i = 0; i < 1000000; i++)
    string path = Request.PhysicalApplicationPath + "Dir\\File.gz";

MapPath usage: C#

// Find physical path with MapPath.
for (int i = 0; i < 1000000; i++)
    string path = Server.MapPath("~/") + "Dir\\File.gz";

Benchmark result: 100 million tests

PhysicalApplicationPath:  109 ms
		MapPath: 3290 ms

Why is PhysicalApplicationPath faster? It does less. I looked into the IL. MapPath results in a long chain of method calls. And the "this._appPhysicalPath" field is used.

So: This is probably what PhysicalApplicationPath uses. MapPath just uses PhysicalApplicationPath, but adds a lot of extra complexity.

My experiments showed that the longer and more complex the path you pass to MapPath, the slower it gets. By replacing MapPath, I improved the overall IO time in benchmarks by 10%.

So: I was surprised to find that it was increasing my total IO time by a substantial amount—more than 10%.

Therefore: In most programs, you should prefer using PhysicalApplicationPath if at all possible.

We looked at the PhysicalApplicationPath property in the ASP.NET Framework. This is a gem that can really help your ASP.NET application function more precisely and faster.