C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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\
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;
Response.Write(path);
// Will write a result similar to the above table's example.
}
}
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
So: This is probably what PhysicalApplicationPath uses. MapPath just uses PhysicalApplicationPath, but adds a lot of extra complexity.
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.