Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 307
Manu Avatar asked Nov 09 '08 09:11

Manu


People also ask

What is server MapPath?

The MapPath method maps the specified relative or virtual path to the corresponding physical directory on the server.

What is server MapPath in MVC?

Many times we need to know the full path of remote server where we are hosting or the exact location of file but if we don't how we can't. Actually we have MapPath method which maps the specified relative or virtual path to the corresponding physical directory on the web server.

What is MapPath in C#?

MapPath Method: It's a static method in System. Web assembly that Maps a virtual path to a physical path on the server. It doesn't require a reference to HttpContext .

Is a physical path but a virtual path was expected?

Generally this physical and virtual path problem occurred whenever we refer “Server. MapPath” value multiple times while using folder path in applications. To solve this e:is a physical path but a virtual path was expected we need to use Server.


2 Answers

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.
like image 196
splattne Avatar answered Oct 05 '22 09:10

splattne


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

like image 31
dav_i Avatar answered Oct 05 '22 09:10

dav_i