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

dav_j
dav_j
4 Points
2 Posts

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

Thanks in advance

Views: 8645
Total Answered: 1
Total Marked As Answer: 0
Posted On: 20-Apr-2016 05:10

Share:   fb twitter linkedin
Answers
Rahul Maurya
Rahul M...
4960 Points
31 Posts
         

Server.MapPath method Returns the physical file path and takes input parameter as virtual path.

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

For An example:

Suppose you pointed a web site application (https://www.myexample.com/) to

C:\Inetpub\wwwroot

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

D:\MyWebApps\shop

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

https://www.myexample.com/shop/products/GetMyProduct.aspx?id=2342
then:

  • Server.MapPath(".") will return D:\MyWebApps\shop\products
  • Server.MapPath("..") will return D:\MyWebApps\shop
  • Server.MapPath("~") will return D:\MyWebApps\shop
  • Server.MapPath("/") will return C:\Inetpub\wwwroot
  • Server.MapPath("/shop") will return D:\MyWebApps\shop
  • Server.MapPath(null) or Server.MapPath("") will return D:\MyWebApps\shop\products
Posted On: 02-May-2016 00:36
 Log In to Chat