TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

ASP.NET PhysicalApplicationPath

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. From Microsoft: "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; Response.Write(path); // 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.MapPath
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.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf