By Manu


2008-11-09 09:48:03 8 Comments

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

3 comments

@Vaibhav_Welcomes_You 2018-01-08 10:49:21

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

@splattne 2008-11-09 10:01:15

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.

@gbn 2010-05-10 15:16:34

Excellent. We've been battling with Server.Bloody.MapPath. Thanks

@skolima 2012-07-25 14:57:47

You will be better off using HostingEnvironment.MapPath as it doesn't require HttpContext: stackoverflow.com/q/944219/3205

@dav_i 2013-07-12 13:49:35

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(".")

Related Questions

Sponsored Content

62 Answered Questions

[SOLVED] Get the source directory of a Bash script from within the script itself

  • 2008-09-12 20:39:56
  • Jiaaro
  • 1531864 View
  • 4568 Score
  • 62 Answer
  • Tags:   bash directory

30 Answered Questions

[SOLVED] How can I add an empty directory to a Git repository?

  • 2008-09-22 16:41:03
  • Laurie Young
  • 856609 View
  • 3955 Score
  • 30 Answer
  • Tags:   git directory git-add

21 Answered Questions

[SOLVED] How do I list all files of a directory?

  • 2010-07-08 19:31:22
  • duhhunjonn
  • 3541892 View
  • 3474 Score
  • 21 Answer
  • Tags:   python directory

15 Answered Questions

30 Answered Questions

[SOLVED] Setting environment variables on OS X

6 Answered Questions

[SOLVED] Node.js check if path is file or directory

13 Answered Questions

[SOLVED] Find current directory and file's directory

  • 2011-02-28 01:51:21
  • John Howard
  • 2589503 View
  • 1921 Score
  • 13 Answer
  • Tags:   python directory

4 Answered Questions

25 Answered Questions

[SOLVED] ASP.NET Web Site or ASP.NET Web Application?

2 Answered Questions

[SOLVED] What is the difference between Server.MapPath and HostingEnvironment.MapPath?

  • 2009-06-03 11:28:04
  • empi
  • 72327 View
  • 167 Score
  • 2 Answer
  • Tags:   c# asp.net

Sponsored Content