Server.MapPath("."), Server.MapPath("~"), Server.MapPath(@"\"), Server.MapPath("/"). What is the difference?
Asked Answered
E

4

475

Can anyone explain the difference between Server.MapPath("."), Server.MapPath("~"), Server.MapPath(@"\") and Server.MapPath("/")?

Esemplastic answered 9/11, 2008 at 9:48 Comment(0)
P
842

Server.MapPath specifies the relative or virtual path to map to a physical directory.

  • Server.MapPath(".")1 returns the current physical directory of the file (e.g. aspx) being executed
  • Server.MapPath("..") returns the parent directory
  • Server.MapPath("~") returns the physical path to the root of the application
  • Server.MapPath("/") returns the physical path to the root of the domain name (is not necessarily the same as the root of the application)

An example:

Let's say you pointed a web site application (http://www.example.com/) to

C:\Inetpub\wwwroot

and installed your shop application (sub web as virtual directory in IIS, marked as application) in

D:\WebApps\shop

For example, if you call Server.MapPath() in following request:

http://www.example.com/shop/products/GetProduct.aspx?id=2342

then:

  • Server.MapPath(".")1 returns D:\WebApps\shop\products
  • Server.MapPath("..") returns D:\WebApps\shop
  • Server.MapPath("~") returns D:\WebApps\shop
  • Server.MapPath("/") returns C:\Inetpub\wwwroot
  • Server.MapPath("/shop") returns D:\WebApps\shop

If Path starts with either a forward slash (/) or backward slash (\), the MapPath() returns a path as if Path was a full, virtual path.

If Path doesn't start with a slash, the MapPath() returns a path relative to the directory of the request being processed.

Note: in C#, @ is the verbatim literal string operator meaning that the string should be used "as is" and not be processed for escape sequences.

Footnotes

  1. Server.MapPath(null) and Server.MapPath("") will produce this effect too.
Panter answered 9/11, 2008 at 10:1 Comment(3)
Excellent. We've been battling with Server.Bloody.MapPath. ThanksHardwick
You will be better off using HostingEnvironment.MapPath as it doesn't require HttpContext: https://mcmap.net/q/81156/-what-is-the-difference-between-server-mappath-and-hostingenvironment-mappath/3205Byrdie
Two questions, as follows: (1.) Does the ASP Classic version of Server.MapPath work the same way as described above? AND (2.) I have an SSD drive that I installed in a server, and it's drive letter is "G:" The website is on drive letter "E:" I would like the IMAGES directory (and perhaps others) to reside on the G: drive (for speed), but Server.MapPath returns E: when used in code. It is already a virtual directory that points to the E: drive. If I point it to the G: drive, how do I inform Server.MapPath to return the proper drive letter to the IMAGES folder?Zwickau
S
25

Just to expand on @splattne's answer a little:

MapPath(string virtualPath) calls the following:

public string MapPath(string virtualPath)
{
    return this.MapPath(VirtualPath.CreateAllowNull(virtualPath));
}

MapPath(VirtualPath virtualPath) in turn calls MapPath(VirtualPath virtualPath, VirtualPath baseVirtualDir, bool allowCrossAppMapping) which contains the following:

//...
if (virtualPath == null)
{
    virtualPath = VirtualPath.Create(".");
}
//...

So if you call MapPath(null) or MapPath(""), you are effectively calling MapPath(".")

Standard answered 12/7, 2013 at 13:49 Comment(0)
F
4

1) Server.MapPath(".") -- Returns the "Current Physical Directory" of the file (e.g. aspx) being executed.

Ex. Suppose D:\WebApplications\Collage\Departments

2) Server.MapPath("..") -- Returns the "Parent Directory"

Ex. D:\WebApplications\Collage

3) Server.MapPath("~") -- Returns the "Physical Path to the Root of the Application"

Ex. D:\WebApplications\Collage

4) Server.MapPath("/") -- Returns the physical path to the root of the Domain Name

Ex. C:\Inetpub\wwwroot

Flyspeck answered 8/1, 2018 at 10:49 Comment(0)
A
0

Working Example, hope this helps show a way to use MapPath with more than just a "/". We are combining "/" and "~".

  1. string lotMapsUrl = "~/WebFS/Transport/Maps/Lots/"; --- just get the long URL into a variable
  2. string lotMapsDir = Server.MapPath(lotMapsUrl); --- get our full physical path to this location
  3. string[] files = Directory.GetFiles(lotMapsUrl); --- go grab a list of the files from the physical path.
Acanthoid answered 31/7, 2023 at 17:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.