Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
170 views
in Technique[技术] by (71.8m points)

c# - Using ServerManager to create Application within Application

I'm using ServerManager (Microsoft.Web.Administration.dll) to create an Application within a website on IIS 7. I need to create an application within this application or convert a virtual directory to an application (like the iis manager feature right-click -> convert to application) How is this doable? I found very little documentation regarding this lib, and none of it referred to this particular functionality. Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

alt text

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:

alt text

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:

alt text

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:

alt text

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:

alt text

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:

alt text

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:

alt text

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:

alt text

Add Application to


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...