Server.MapPath

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

  • Server.MapPath(“.”) 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 the root of your domain

C:Inetpubwwwroot

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

D:WebAppsshop 

If, you call Server.MapPath in following request:

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

then,

  • Server.MapPath(“.”) returns D:WebAppsshopproducts
  • Server.MapPath(“..”) returns D:WebAppsshop
  • Server.MapPath(“~”) returns D:WebAppsshop
  • Server.MapPath(“/”) returns C:Inetpubwwwroot
  • Server.MapPath(“/shop”) returns D:WebAppsshop

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

If Path doesn’t start with a slash, the MapPath method 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.

Source: Link

Leave a Comment