Sunday, 14 April 2013

How to use server.mappath


How to use 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.sitename.com/) to

C:\Inetpub\wwwroot
and installed your 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.sitename.com/category/products/GetProduct.aspx?id=1
then:

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