Dot Net Perls

MapPath Resolves Virtual and Physical Paths - ASP.NET

by Sam Allen

Problem

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. We want paths that indicate the physical file location. These are called physical paths.

Solution: C#

What we need to look at first is virtual paths in ASP.NET. The tilde character (~) indicates the root of a virtual path. (We need the tilde because otherwise ASP.NET couldn't figure out if a path is absolute or relative.) Let's look at some virtual paths and what they might map to.

Virtual pathPhysical path
~/App_Data/Sample.xmlC:\Website\Files\Sample.xml
~/C:\Website\Default.aspx
~/Map.txtC:\Website\Map.txt

How can I call MapPath?

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.

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 easily.
/// </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");
    }
}

Is Server.MapPath different?

No. 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.

I want to see another example.

Examples are what make the Internet really shine, so I will oblige you here. There is an entire article here about XElement examples, and I will use some of that code here. XElement is an XML object that can open a file, much like StreamReader.

class SiteStructure
{
    XElement _x;

    /// <summary>
    /// Load the Map1.xml file that is in App_Data directly into the in-memory
    /// object XElement _x.
    /// </summary>
    public SiteStructure()
    {
        string mapLoc = HttpContext.Current.Request.MapPath("~/App_Data/Map1.xml");
        _x = XElement.Load(mapLoc);
    }
}

Something else is wrong.

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. In fact, MapPath is very simple and unless you have a typo in the argument, it won't cause you any problems. Keep looking for more bugs.

Any performance concerns?

Possibly. MapPath's 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.

Conclusion

MapPath is a method that the framework gives us to resolve 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.

Dot Net Perls
About
Sitemap
Source code
RSS
ASP.NET
Page Load Event and AJAX
Custom ASHX Handler Tutorial
Cache Examples and Overview
System.Timers for ASP.NET Website Tutorial
HtmlTextWriter Use
Recent
Pi
NGEN Installer Class
List Element Equality
DateTime Tips and Tricks
Remove HTML Tags From String
© 2008 Sam Allen. All rights reserved.