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 MapPath: Virtual and Physical Paths

Use MapPath in ASP.NET. MapPath returns physical paths and file locations.
MapPath is an important ASP.NET method. It resolves virtual paths and physical paths. We run the ASP.NET development server on a local machine. Its paths are not the same as they are on a server. The MapPath method handles this.Path
As a reminder, in ASP.NET the "~" tilde indicates the root of a virtual path. We need the tilde because otherwise ASP.NET can't figure out if a path is absolute or relative. Let's look at some virtual paths and what they might map to.
Virtual paths ~/App_Data/Sample.xml ~/ ~/Map.txt Physical paths C:\Website\Files\Sample.xml C:\Website\Default.aspx C:\Website\Map.txt
Example. You can call MapPath in any C# file in your ASP.NET website. You may want to include the System.Web namespace first, but this is not required. Make sure you are looking at a C# file in your ASP.NET project.

Note: The Server.MapPath does the same thing as the Request.MapPath method. In this example, the two versions will do the same thing.

Note 2: There may be some differences in different usage scenarios, but in those cases a more detailed guide would be helpful.

Also: The two methods (Server.MapPath and Request.MapPath) are interchangeable in most ASP.NET projects.

Example code that uses MapPath: C# using System; using System.Web; /// <summary> /// This is an example code-behind file you can put in App_Code. /// It shows examples of using MapPath in code-behind. /// </summary> public class Example { public Example() { // This will locate the Example.xml file in the App_Data folder. // ... (App_Data is a good place to put data files.) string a = HttpContext.Current.Server.MapPath("~/App_Data/Example.xml"); // This will locate the Example.txt file in the root directory. // ... This can be used in a file in any directory in the application. string b = HttpContext.Current.Request.MapPath("~/Example.txt"); } }
Discussion. You can use the MapPath method to access many different paths on the server. There is an entire article here about XElement examples. XElement is an XML object that can open a file, much like StreamReader.XElementStreamReader
Also, we note that if you are using a virtual shared host, there may be problems in your code related to file permissions and security checks. The problem may not be MapPath at all.

Note: MapPath is very simple and unless you have a typo in the argument, it won't cause you any problems.

Next, you might be interested to find that MapPath performance is over 1000 times slower than a simple string append. It could be worthwhile to cache the paths, in a technique similar to that in my article about appSettings caches.appSettings
Summary. We looked at the MapPath method in the C# programming language. MapPath is a method that resolves virtual paths to machine paths. It has great utility for XML and some other data files.

Tip: MapPath can work as a bridge between website-specific virtual paths, and a physical path that most .NET IO methods will require.

PhysicalApplicationPath
© 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