In Focus

Server.MapPath() In ASP.NET

This article will demonstrate how to use and why to use Server.MapPath() in ASP.Net web applications.

Introduction and Demonstration

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. Usually any web server never allows to access in any path if we don't have proper permission. Even we can't list the directories or file as we list in DOS like,

Server.MapPath() In ASP.NET
As in above picture, we can list on web server via remotely without proper permission because of security reason. But we can see the full path where the particular page exists as in example given below.

Server.MapPath() In ASP.NET
In above example, coded (given below) file is being hosted at remote server and it is displaying (mapping) the physical location (path) of my hosting server. Here is my coding I have used.
  1. protected void Page_Load(object sender, EventArgs e) {    
  2.     Response.Write(Server.MapPath("."));    
  3.     Response.Write("<br>");    
  4.     Response.Write(Server.MapPath(""));    
  5.     Response.Write("<br>");    
  6.     Response.Write(Server.MapPath("~"));    
  7. }