
You have to determine physical paths on your server in ASP.NET. There are different ways of resolving paths, including the PhysicalApplicationPath property. Here we review physical paths and virtual paths, and how to convert and combine them using the C# programming language.
Here we use Request.PhysicalApplicationPath and then append physical paths to it. From MSDN: "It gets the physical file system path of the currently executing server application's root directory." In other words, PhysicalApplicationPath will return the location on the server that your web site is stored. It won't return any URLs for the browser.
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 and can be accessed in a few different ways. On your aspx page, you can access the PhysicalApplicationPath like this:
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;
Response.Write(path);
// Will write a result similar to the above table's example.
}
}Description. The Request object can also be accessed with "HttpContext.Current.Request.MapPath." It is sometimes best to store the Request in a variable instead of repeatedly accessing the property.
It is faster and serves a more precise purpose. MapPath can be used to resolve the root path, with MapPath("~/"). However, that is much slower. My testing indicated that there are big performance differences between the ways of finding the application path.
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 more efficient? It does less. I looked into the IL code here and MapPath calls result in a long chain of method calls. At some point, the "this._appPhysicalPath" field is used, which is probably what PhysicalApplicationPath uses too.
My experiments showed that the longer and more complex the path you pass to MapPath, the slower it gets. By replacing the MapPath call in my code with PhysicalApplicationPath, I improved the overall IO time in benchmarks by 10%.
Tip: I was surprised to find that it was increasing my total IO time by a substantial amount—more than 10%. You should prefer using PhysicalApplicationPath if at all possible.

We looked at the PhysicalApplicationPath property in the ASP.NET Framework. PhysicalApplicationPath is a gem that can really help your ASP.NET application function more precisely and faster. It is also self-documenting, because the function name describes its usage in your code.
ASP.NET Tutorials