The way to do this is to manipulate the Site.Applications
collection which is a flattened tree of all the applications in your site.
For the sake of these examples we'll assume a site called "MySite" where the content is located on the local hard disk at: d:mysitewww
. The site's IIS number is 3
and the site resides in its own application pool also called "MySite".
We'll also assume the following folder structure for the site
To start with we get the site we want to add an application to, we'll use the variable site
throughout:
// Get my site
Site site = serverManager.Sites.First(s => s.Id == 3);
The root "/" application:
Every site has a "root" application. If we open applicationHost.config
located in %systemroot%windowssystem32inetsrvconfig
and locate the <site>
node for our site we see the following:
<site name="MySite" id="3">
<application path="/" applicationPool="MySite">
<virtualDirectory path="/" physicalPath="d:mysitewww" />
</application>
</site>
Each <site>
contains a collection of <application>
's. There will always be at least one application which defines the root application, /
.
The applicationPool
attribute specifies which application pool to use.
Note that that there is a single child element: virtualDirectory
.
Every application
has a child collection of virtualDirectory
elements and there will usually be at least one element in this collection.
The default <virtualDirectory>
within the root application tells us:
- this this is the root (
path="/"
) and
- that it's physically located on the file system at
d:MySitewww
(physicalPath="d:MySitewww"
).
The path
of each virtualDirectory
is relative 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 to somewhere else on the filesystem we'd do:
Application rootApp = site.Applications.First(a => a.Path == "/");
rootApp.VirtualDirectories.Add("/vdir_1", @"D:MySiteother_content");
serverManager.CommitChanges();
The resultant configuration looks like:
<site name="MySite" id="3">
<application path="/" applicationPool="MySite">
<virtualDirectory path="/" physicalPath="D:MySitewww" />
<virtualDirectory path="/vdir_1" physicalPath="D:MySiteother_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'd do:
root.VirtualDirectories.Add("/vdir_1/sub_dir1", @"d:MySitemore_content");
this results in:
<site name="MySite" id="3">
<application path="/" applicationPool="MySite">
<virtualDirectory path="/" physicalPath="D:MySitewww" />
<virtualDirectory path="/vdir_1" physicalPath="D:MySiteother_content" />
<virtualDirectory path="/vdir_1/sub_dir1" physicalPath="D:MySitemore_content" />
</application>
</site>
IIS Manager:
There's a couple things to keep in mind when adding virtual directories:
- As mentioned, the virtual
path
is always relative to the parent application path
- The last part of a virtual
path
e.g. /vdir_1
and .../sub_dir1
becomes the name of the virtual directory
- It's perfectly legal to have more than one virtual directory point to the same physical folder
- With the exception of the name part of a virtual directory path, all parts of the path should exist either as physical paths or as virtual paths within the website root (
d:MySitewww
). i.e. the path
should be able to overlay something that is already there otherwise a virtual directory won't be visible in IIS manager.
Regarding that last point, for example, we don't have a physical folder or virtual directory called /vdir_2
but the following code is perfectly legal:
root.VirtualDirectories.Add("/vdir_2/sub_dir1", @"d:MySiteeven_more_content");
You won't see /vdir_2/sub_dir1
show up in IIS manager but it is legal and you can actually browse to it. We can also see it in applicationHost.config
:
<site name="MySite" id="3">
<application path="/" applicationPool="MySite">
<virtualDirectory path="/" physicalPath="D:MySitewww" />
<virtualDirectory path="/vdir_1" physicalPath="D:MySiteother_content" />
<virtualDirectory path="/vdir_1/sub_dir1" physicalPath="D:MySitemore_content" />
<virtualDirectory path="/vdir_2/sub_dir1" physicalPath="D:MySiteeven_more_content" />
</application>
</site>
Convert Folder to Application:
If you just uploaded an ASP.NET application to the /app_1
folder in your site and you want to turn this into its own Application we do this:
Application app = site.Applications.Add("/app_1", @"d:MySitewwwapp_1");
// set application pool, otherwise it'll run in DefaultAppPool
app.ApplicationPoolName = "MySite";
serverManager.CommitChanges();
In applicationHost.config
we can see a new <application>
element has been added:
<site name="MySite" id="3">
<application path="/" applicationPool="MySite">
<virtualDirectory path="/" physicalPath="D:MySitewww" />
<virtualDirectory path="/vdir_1" physicalPath="D:MySiteother_content" />
<virtualDirectory path="/vdir_1/sub_dir1" physicalPath="D:MySitemore_content" />
<virtualDirectory path="/vdir_2/sub_dir1" physicalPath="D:MySiteeven_more_content" />
</application>
<application path="/app_1" applicationPool="MySite">
<virtualDirectory path="/" physicalPath="d:MySitewwwapp_1" />
</application>
</site>
In IIS we see:
This is the equivalent of doing right-click "Convert to Application".
Add Application to Existing Application:
Adding an application as a child of an existing application is very simple. Say we want to make /app_1/sub_app_1
a sub application of /app_1
:
We would simply do:
Application app =
site.Applications.Add("/app_1/sub_app_1", @"d:mysitewwwapp_1sub_app_1");
app.ApplicationPoolName ="MySite";
The resultant configuration would look like:
<site name="MySite" id="3">
<application path="/" applicationPool="MySite">
<virtualDirectory path="/" physicalPath="D:MySitewww" />
<virtualDirectory path="/vdir_1" physicalPath="D:MySiteother_content" />
<virtualDirectory path="/vdir_1/sub_dir1" physicalPath="D:MySitemore_content" />
<virtualDirectory path="/vdir_2/sub_dir1" physicalPath="D:MySiteeven_more_content" />
</application>
<application path="/app_1" applicationPool="MySite">
<virtualDirectory path="/" physicalPath="d:MySitewwwapp_1" />
</application>
<application path="/app_1/sub_app_1" applicationPool="MySite">
<virtualDirectory path="/" physicalPath="d:mysitewwwapp_1sub_app_1" />
</application>
</site>
In IIS:
Add Virtual Directory to Application:
Now if we wanted to add a virtual directory to this application we would do:
Application app = site.Applications.First(a => a.Path == "/app_1");
app.VirtualDirectories.Add("/vdir_1", @"d:MySiteother_content");
In applicationHost.config
we can see a new <virtualDirectory>
element has been added:
<site name="MySite" id="3">
<application path="/" applicationPool="MySite">
<virtualDirectory path="/" physicalPath="D:MySitewww" />
<virtualDirectory path="/vdir_1" physicalPath="D:MySiteother_content" />
<virtualDirectory path="/vdir_1/sub_dir1" physicalPath="D:MySitemore_content" />
<virtualDirectory path="/vdir_2/sub_dir1" physicalPath="D:MySiteeven_more_content" />
</application>
<application path="/app_1" applicationPool="MySite">
<virtualDirectory path="/" physicalPath="d:MySitewwwapp_1" />
<virtualDirectory path="/vdir_1" physicalPath="d:MySiteother_content" />
</application>
</site>
In IIS we see:
Again it is important to note that the virtual path /vdir1
is always relative to the path of the containing application.
Convert Existing Virtual Directory to Application:
What if we wanted to convert the virtual directory we just created (/app_1/vdir1
) to an application? We'd need to do this in two steps:
// Get the application
Application app_1 = site.Applications.First(a => a.Path == "/app_1");
// Find the virtual directory
VirtualDirectory vdir_1 = app_1.VirtualDirectories.First(v => v.Path == "/vdir_1");
// Remove it from app_1
app_1.VirtualDirectories.Remove(vdir_1);
// Create our application
Application vdir_1_app = site.Applications.Add("/app_1/vdir_1", vdir_1.PhysicalPath);
// set application pool, otherwise it'll run in DefaultAppPool
vdir_1_app.ApplicationPoolName = "MySite";
serverManager.CommitChanges();
The resultant applicationHost.config
looks like:
<site name="MySite" id="3">
<application path="/" applicationPool="MySite">
<virtualDirectory path="/" physicalPath="D:MySitewww" />
<virtualDirectory path="/vdir_1" physicalPath="D:MySiteother_content" />
<virtualDirectory path="/vdir_1/sub_dir1" physicalPath="D:MySitemore_content" />
<virtualDirectory path="/vdir_2/sub_dir1" physicalPath="D:MySiteeven_more_content" />
</application>
<application path="/app_1" applicationPool="MySite">
<virtualDirectory path="/" physicalPath="d:MySitewwwapp_1" />
</application>
<application path="/app_1/vdir_1" applicationPool="MySite">
<virtualDirectory path="/" physicalPath="d:MySiteother_content" />
</application>
</site>
In IIS Manager we see:
Add Application to