In the last blog post, we have discussed in detail about
UNION ALL Operators in SQL Server. You can read that article here. In this article, we will go over mapping virtual path to physical path using
Server.MapPath method in ASP.NET.
First of all, let’s understand what is meant by physical path and virtual path. Here, we have a simple web application project. Within the project, I have a page called PageInRootDirectory.aspx. First, let’s understand what is meant by root directory or root web application directory.
This project is physically created on the hard disc. How do we find out the folder in which this project is physically created? For that, right click on the folder and select Open Folder in Windows Explorer. It should open up the physical folder where this project is created. If we look at it, we can see that it is present in D:/SampleWeb/SampleWeb.
So for this web application project, the root directory is D:/SampleWeb/SampleWeb. This is the physical path where the file PageInRootDirectory.aspx is present. If the user has to view this file in a browser, then right click on that file and select View in Browser. Now we should see the file in the browser window. The address which is seen in the browser window is the virtual path, which means this path doesn’t really exist. This is being served by the web server.
Now we will see how
Server.MapPath method is used to map the virtual path to a physical path.
What is Server.MapPath Method?
- Server.MapPath method returns the physical path for a given virtual path.
- This method can be used in several different ways, depending on the characters that we use in the virtual path.
Let’s understand this with an example.
First of all, create a web application with the following structure. In the root directory, we have Categories folder. Inside Categories folder, there is another folder called Electronics. Inside Electronics folder, there is a page called PageInElectronicsFolder.aspx.
Similarly, there is a Data folder in root directory. Inside that, there is a Countries folder. Inside Countries folder, there is an XML file called Countries.xml.
From the above hierarchy, it is clear that PageInElectronicsFolder.aspx resides in Electronics folder which in turn resides in Categories folder in root directory. Now let’s go to PageInElectronicsFolder.aspx.cs and write the code like below:
While we try to write
Server.MapPath method, the intellisense itself clearly describes what the method is.
Let’s run this and see what will happen.
The output will give the current directory of the executing page.
. symbol will give us the physical path of the current executing page.
Now I want to get the Categories folder. That is, the parent directory of the executing page. For this, I have to pass
.. symbol to
Server.MapPath method. Write the code like below:
Run the application and see what will happen. The output will give the parent directory of the executing page.
So we got the parent directory of the executing page as well. Now how do we get into the root directory of any web application project? There are 2 ways to do that. The commonly used way is to use ~ character. Write the code like below:
While running the application, we will get the root directory as output.
So these are the 3 symbols commonly used for mapping a virtual path to a physical path.
Server.MapPath(“.”) - Returns the current physical directory of the page that are running
Server.MapPath(“..”) – Returns the parent physical directory of the page that are running
Server.MapPath(“~”) - Returns the physical path of the root directory of the application
The other way to get the physical path of the root directory is to use ../.. symbol. It makes sense as well. We know that in order to get parent directory of the executing page, .. symbol is used. If we use .. symbol again, we will get the parent directory of this directory which is nothing but the root directory of the web application project. Write the code like below:
While running the application, we will again get the physical path of the root directory.
Now if we look at the PageInRootDirectory.aspx, we can see that the page is already in the root directory. Simply copy and paste the whole code from PageInElectronicsFolder.aspx.cs to this page and see what will happen.
But we know that the page resides on the root directory of the web application project and there is no parent directory for this page. So the line of code
Response.write(Server.MapPath(“..”) ) may throw an error.
The error is because we are already in the root directory and are still trying to navigate out of the root directory.
Now let’s hide the lines of codes which may cause error and rerun the application.
While running the application, in both the cases, we will get the root directory of the application.
Reference: Arun Ramachandran (http://BestTEchnologyBlog.Com)