The way to do this is to manipulate the Site.Applications collection, which is a smoothed tree of all applications on your site.
For these examples, we take a site called "MySite", where the content is on the local hard drive: d:\mysite\www . The site number is IIS 3 , and the site is in its own application pool, also called "MySite".
We will also assume the following folder structure for the site

First, we will add the site to which we want to add the application, we will use the site variable:
Root application "/":
Each site has a βrootβ application. If we open applicationHost.config located in %systemroot%\windows\system32\inetsrv\config and find the <site> node for our site, we will see the following:
<site name="MySite" id="3"> <application path="/" applicationPool="MySite"> <virtualDirectory path="/" physicalPath="d:\mysite\www" /> </application> </site>
Each <site> contains a <application> collection. There will always be at least one application that defines the root application, / .
The applicationPool attribute indicates which application pool to use.
Note that there is only one child: virtualDirectory .
Each application has a child virtualDirectory element virtualDirectory , and this collection will usually have at least one element.
By default, <virtualDirectory> in the root application tells us:
- this is the root (
path="/" ) and - that it is physically located on the file system in
d:\mysite\www ( physicalPath="d:\MySite\www" ).
path each virtualDirectory refers to the path specified in the parent application path.
Adding a virtual directory:
If we wanted to add a virtual directory to the "site root" mapped somewhere else in the file system, we would do:
Application rootApp = site.Applications.First(a => a.Path == "/"); rootApp.VirtualDirectories.Add("/vdir_1", @"D:\MySite\other_content"); serverManager.CommitChanges();
The resulting configuration is as follows:
<site name="MySite" id="3"> <application path="/" applicationPool="MySite"> <virtualDirectory path="/" physicalPath="D:\MySite\www" /> <virtualDirectory path="/vdir_1" physicalPath="D:\MySite\other_content" /> </application> </site>
And we see this in IIS Manager:

Adding a virtual directory to an existing virtual directory:
If we wanted to add a child virtual directory to vdir1 , we would do:
root.VirtualDirectories.Add("/vdir_1/sub_dir1", @"d:\MySite\more_content");
it leads to:
<site name="MySite" id="3"> <application path="/" applicationPool="MySite"> <virtualDirectory path="/" physicalPath="D:\MySite\www" /> <virtualDirectory path="/vdir_1" physicalPath="D:\MySite\other_content" /> <virtualDirectory path="/vdir_1/sub_dir1" physicalPath="D:\MySite\more_content" /> </application> </site>
IIS Manager:

There are a couple of things to keep in mind when adding virtual directories:
- As already mentioned, the virtual
path always refers to the parent application path - The last part of the virtual
path , for example. /vdir_1 and .../sub_dir1 becomes the name of the virtual directory - It is completely legal to have more than one virtual directory in one physical folder
- With the exception of the part of the path name to the virtual directory, all parts of the path must exist either as physical paths or as virtual paths in the root of the site (
d:\mysite\www ). that is, path must be able to overlap what already exists, otherwise the virtual directory will not be displayed in IIS Manager.
As for the last point, for example, we donβt have a physical folder or virtual directory named /vdir_2 , but the following code is completely legal:
root.VirtualDirectories.Add("/vdir_2/sub_dir1", @"d:\MySite\even_more_content");
You will not see /vdir_2/sub_dir1 in IIS Manager, but it is legal and you can actually go to it. We can also see this in applicationHost.config :
<site name="MySite" id="3"> <application path="/" applicationPool="MySite"> <virtualDirectory path="/" physicalPath="D:\MySite\www" /> <virtualDirectory path="/vdir_1" physicalPath="D:\MySite\other_content" /> <virtualDirectory path="/vdir_1/sub_dir1" physicalPath="D:\MySite\more_content" /> <virtualDirectory path="/vdir_2/sub_dir1" physicalPath="D:\MySite\even_more_content" /> </application> </site>
Convert folder to application:
If you just downloaded the ASP.NET application to the /app_1 folder on your site and want to turn it into your application, we do the following:
Application app = site.Applications.Add("/app_1", @"d:\MySite\www\app_1"); // set application pool, otherwise it'll run in DefaultAppPool app.ApplicationPoolName = "MySite"; serverManager.CommitChanges();
In applicationHost.config we will see a new <application> element:
<site name="MySite" id="3"> <application path="/" applicationPool="MySite"> <virtualDirectory path="/" physicalPath="D:\MySite\www" /> <virtualDirectory path="/vdir_1" physicalPath="D:\MySite\other_content" /> <virtualDirectory path="/vdir_1/sub_dir1" physicalPath="D:\MySite\more_content" /> <virtualDirectory path="/vdir_2/sub_dir1" physicalPath="D:\MySite\even_more_content" /> </application> <application path="/app_1" applicationPool="MySite"> <virtualDirectory path="/" physicalPath="d:\MySite\www\app_1" /> </application> </site>
In IIS we see:

This is equivalent to right-clicking Convert to Application.
Add an application to an existing application:
Adding an application as a child of an existing application is very simple. Suppose we want to make /app_1/sub_app_1 helper application /app_1 :

We would just do:
Application app = site.Applications.Add("/app_1/sub_app_1", @"d:\mysite\www\app_1\sub_app_1"); app.ApplicationPoolName ="MySite";
The resulting configuration will look like this:
<site name="MySite" id="3"> <application path="/" applicationPool="MySite"> <virtualDirectory path="/" physicalPath="D:\MySite\www" /> <virtualDirectory path="/vdir_1" physicalPath="D:\MySite\other_content" /> <virtualDirectory path="/vdir_1/sub_dir1" physicalPath="D:\MySite\more_content" /> <virtualDirectory path="/vdir_2/sub_dir1" physicalPath="D:\MySite\even_more_content" /> </application> <application path="/app_1" applicationPool="MySite"> <virtualDirectory path="/" physicalPath="d:\MySite\www\app_1" /> </application> <application path="/app_1/sub_app_1" applicationPool="MySite"> <virtualDirectory path="/" physicalPath="d:\mysite\www\app_1\sub_app_1" /> </application> </site>
In IIS:

Add a virtual directory to the application:
Now, if we wanted to add a virtual directory to this application, we will do the following:
Application app = site.Applications.First(a => a.Path == "/app_1"); app.VirtualDirectories.Add("/vdir_1", @"d:\MySite\other_content");
In applicationHost.config we will see a new <virtualDirectory> element:
<site name="MySite" id="3"> <application path="/" applicationPool="MySite"> <virtualDirectory path="/" physicalPath="D:\MySite\www" /> <virtualDirectory path="/vdir_1" physicalPath="D:\MySite\other_content" /> <virtualDirectory path="/vdir_1/sub_dir1" physicalPath="D:\MySite\more_content" /> <virtualDirectory path="/vdir_2/sub_dir1" physicalPath="D:\MySite\even_more_content" /> </application> <application path="/app_1" applicationPool="MySite"> <virtualDirectory path="/" physicalPath="d:\MySite\www\app_1" /> <virtualDirectory path="/vdir_1" physicalPath="d:\MySite\other_content" /> </application> </site>
In IIS we see:

Again, it is important to note that the virtual path /vdir1 always refers to the path of the containing application.
Convert an existing virtual directory to an application:
What if we want to convert the newly created virtual directory ( /app_1/vdir1 ) into the application? We must do this in two stages:
The result of applicationHost.config as follows:
<site name="MySite" id="3"> <application path="/" applicationPool="MySite"> <virtualDirectory path="/" physicalPath="D:\MySite\www" /> <virtualDirectory path="/vdir_1" physicalPath="D:\MySite\other_content" /> <virtualDirectory path="/vdir_1/sub_dir1" physicalPath="D:\MySite\more_content" /> <virtualDirectory path="/vdir_2/sub_dir1" physicalPath="D:\MySite\even_more_content" /> </application> <application path="/app_1" applicationPool="MySite"> <virtualDirectory path="/" physicalPath="d:\MySite\www\app_1" /> </application> <application path="/app_1/vdir_1" applicationPool="MySite"> <virtualDirectory path="/" physicalPath="d:\MySite\other_content" /> </application> </site>
In IIS Manager, we see:

Add the application to the existing virtual directory:
What happens if we want to add an application to a virtual directory, how does it work? In this example, we will add the application to the virtual directory /vdir_1/sub_dir1 , which we created earlier.
Application app = site.Applications.Add("/vdir_1/sub_dir1/app_2", @"d:\mysite\other_content"); app.ApplicationPoolName = "MySite"; serverManager.CommitChanges();
The resulting configuration looks like this:
<site name="MySite" id="3"> <application path="/" applicationPool="MySite"> <virtualDirectory path="/" physicalPath="D:\MySite\www" /> <virtualDirectory path="/vdir_1" physicalPath="D:\MySite\other_content" /> <virtualDirectory path="/vdir_1/sub_dir1" physicalPath="D:\MySite\more_content" /> <virtualDirectory path="/vdir_2/sub_dir1" physicalPath="D:\MySite\even_more_content" /> </application> <application path="/app_1" applicationPool="MySite"> <virtualDirectory path="/" physicalPath="d:\MySite\www\app_1" /> </application> <application path="/app_1/vdir_1" applicationPool="MySite"> <virtualDirectory path="/" physicalPath="d:\MySite\other_content" /> </application> <application path="/vdir_1/sub_dir1/app_2" applicationPool="MySite"> <virtualDirectory path="/" physicalPath="d:\mysite\other_content" /> </application> </site>
In the IIS manager we see:

Convert existing child folder to application:
As a final example, we want to turn /other_apps/sub_app_1 into an application:

Our code looks like this:
Application app = site.Applications.Add("/other_apps/sub_app_1", @"d:\mysite\other_content"); app.ApplicationPoolName="MySite"; serverManager.CommitChanges();
Resulting configuration:
<site name="MySite" id="3"> <application path="/" applicationPool="MySite"> <virtualDirectory path="/" physicalPath="D:\MySite\www" /> <virtualDirectory path="/vdir_1" physicalPath="D:\MySite\other_content" /> <virtualDirectory path="/vdir_1/sub_dir1" physicalPath="D:\MySite\more_content" /> <virtualDirectory path="/vdir_2/sub_dir1" physicalPath="D:\MySite\even_more_content" /> </application> <application path="/app_1" applicationPool="MySite"> <virtualDirectory path="/" physicalPath="d:\MySite\www\app_1" /> </application> <application path="/app_1/vdir_1" applicationPool="MySite"> <virtualDirectory path="/" physicalPath="d:\MySite\other_content" /> </application> <application path="/vdir_1/sub_dir1/app_2" applicationPool="MySite"> <virtualDirectory path="/" physicalPath="d:\mysite\other_content" /> </application> <application path="/other_apps/sub_app_1" applicationPool="MySite"> <virtualDirectory path="/" physicalPath="d:\mysite\other_content" /> </application> </site>
In IIS Manager:

We hope this helps explain the relationship between sites, applications, and virtual directories.