Saturday, March 3, 2012

What is ASP.NET MapPath?

ASP.NET MapPath Resolves Virtual, Physical Paths

You need to use MapPath to resolve virtual paths and physical paths. You run the ASP.NET development server on your local machine, but the paths on it are not the same as they are on your server. Here we use MapPath to find physical paths and file locations, using the C# programming language.

Introduction
First, 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.txtMapPath
For 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 and then add some code that looks similar to parts of the following.

Example code that uses MapPath

using System;
using System.Web;



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");
}
}
Using Server.MapPath. Here we note that the Server.MapPath does the same thing as the Request.MapPath method. In this example, the two versions will do the same thing. There may be some differences in different usage scenarios, but in those cases a more detailed guide would be helpful. The two methods are interchangeable in most ASP.NET projects.

XML files
Here we note that 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.

Virtual hosts security
Here 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. MapPath is very simple and unless you have a typo in the argument, it won't cause you any problems.

Performance
You might be interested to find that MapPath performance is over 1000 times slower than a simple string append. Therefore, it could be worthwhile to cache the paths, in a technique similar to that in my article about appSettings caches.

Summary
MapPath is a method that resolves virtual paths to machine paths. It has great utility for XML and some other data files. It can work as a bridge between website-specific virtual paths, and a physical path that most .NET I/O methods will require.







We collect videos & images from online sites like youtube and some other websites which provide them. And most of the News is also gathered from other online websites. Any material downloaded or otherwise obtained through the use of the service is done at your own discretion and risk and that you will be solely responsible for any damage to your computer system or loss of data that results from the download of any such material. If you feel that the content on the site is illegal or Privacy please contact us at srinuchalla@gmail.com and such videos, images or any Content will be removed with immediate effect.

No comments:

Post a Comment