Wednesday, January 27, 2010

ASP.NET MapPath

MapPath is used 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.

What is MapPath?
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/test.xml
~/
~/Mapfile.txt

Physical paths: C:\Website\Files\test.xml
C:\Website\Default.aspx
C:\Website\Mapfile.txt
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. In fact, MapPath is very simple and unless you have a typo in the argument, it won't cause you any problems

Difference of Server.MapPath and Page.MapPath

both map a virtual path to a physical one..

Page.MapPath: -

Retrieves the physical path that a virtual path, either absolute or relative, or an application-relative path maps to..

Server.MapPath: -

Each file on a Web server has two ways of being accessed - through a virtual path and through a physical path. The virtual path is the path one would enter into their browser's Address bar, for example:

http://www.yourserver.com/someDirectory/someFile.asp

The bold part of the URL above is referred to as the virtual path. The physical path is the actual drive, directory, and filename of a particular file on the Web server. For example, on your computer, if you run Windows 9X, your Win.ini file's physical path is C:\WINDOWS\WIN.INI (assuming you installed Windows on your C: drive).

No comments:

Post a Comment