Click here to Skip to main content
15,891,905 members
Articles / Web Development / ASP.NET

ASP.NET – How to Map Virtual Path to Physical Path?

Rate me:
Please Sign up or sign in to vote.
4.77/5 (11 votes)
10 Feb 2014CPOL4 min read 95.3K   8   2
How to map virtual path to physical path in ASP.NET

In the last blog post, we have discussed in detail about UNION and 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.

1

2

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.

3

4

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.

Solution Explorer

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:

C#
Response.write(Server.MapPath(".")+"<br/>") ;

While we try to write Server.MapPath method, the intellisense itself clearly describes what the method is.

5

6

Let’s run this and see what will happen.

The output will give the current directory of the executing page.

7

So . 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:

C#
Response.write(Server.MapPath("..")+"<br/>") ;

8

Run the application and see what will happen. The output will give the parent directory of the executing page.

9

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:

C#
Response.write(Server.MapPath("~")+"<br/>") ;

10

While running the application, we will get the root directory as output.

11

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:

C#
Response.write(Server.MapPath("../..")+"<br/>") ;

12

While running the application, we will again get the physical path of the root directory.

13

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.

14

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.

15

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.

16

While running the application, in both the cases, we will get the root directory of the application.

17

 Reference: Arun Ramachandran (http://BestTEchnologyBlog.Com)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
Arun Ramachandran is a Software Engineer having hands on experience in different Microsoft Technologies who is presently working in Experion Technologies, India. He has written over 95 articles on the subject on his blog at http://BestTEchnologyBlog.com. Along with 3 years of hands on experience he holds a Master of Computer Applications degree from Cochin University of Science & Technology (CUSAT).

Comments and Discussions

 
QuestionNice Article Pin
Siva Hyderabad9-May-14 20:42
Siva Hyderabad9-May-14 20:42 
AnswerRe: Nice Article Pin
Arun Ramachandran India11-May-14 19:31
Arun Ramachandran India11-May-14 19:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.