使用ServerManager的创建中应用应用 [英] Using ServerManager to create Application within Application

查看:137
本文介绍了使用ServerManager的创建中应用应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ServerManager的(Microsoft.Web.Administration.dll)创建于IIS 7网站中的应用程序。
我需要创建这个应用程序中的应用程序或转换一个虚拟目录的应用程序(如IIS管理器功能右击 - >转换成应用程序)
这是怎么可行?我发现关于这个lib中很少的文件,并没有它提到了这个特殊的功能。
谢谢你。

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.

推荐答案

要做到这一点的方法是操纵 Site.Applications 收藏这是所有的扁平树在您的网站的应用程序。

The way to do this is to manipulate the Site.Applications collection which is a flattened tree of all the applications in your site.

有关的这些例子我们假设一个名为mysite的网站,内容所在的本地硬盘上的缘故: D:\\ mysite的\\ WWW 。该网站的IIS号码是 3 和站点驻留在自己的应用程序池也被称为mysite的。

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:\mysite\www. 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);

根/应用程序:

每个网站都有一个根的应用程序。如果我们打开位于的applicationHost.config 的%systemroot%\\ WINDOWS \\ SYSTEM32 \\ INETSRV \\ CONFIG 并找到<位点GT; 我们的网站,我们看到下面的节点:

Every site has a "root" application. If we open applicationHost.config located in %systemroot%\windows\system32\inetsrv\config 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:\mysite\www" />
  </application>
</site>

每个&LT;网站&GT; 包含&LT的集合;用途&gt; 的。总会有至少一个应用程序定义了应用程序根, /

Each <site> contains a collection of <application>'s. There will always be at least one application which defines the root application, /.

applicationPool 属性指定要使用的应用程序池。

The applicationPool attribute specifies which application pool to use.

需要注意的是,有一个子元素: virtualDirectory

Note that that there is a single child element: virtualDirectory.

每个应用 virtualDirectory 元素的一个子集,而且通常会在至少一种元素在此集合。

Every application has a child collection of virtualDirectory elements and there will usually be at least one element in this collection.

默认&LT; virtualDirectory&GT; 中的根应用程序告诉我们:

The default <virtualDirectory> within the root application tells us:


  • 这可能是根(路径=/)和

  • ,它的在 D物理位置在文件系统上:\\ mysite的\\ WWW physicalPath =D:\\ mysite的\\ www的)。

  • this this is the root (path="/") and
  • that it's physically located on the file system at d:\MySite\www (physicalPath="d:\MySite\www").

路径每个 virtualDirectory 是相对于路径应用指定的路径。

The path of each virtualDirectory is relative to the path specified in the parent application path.

添加一个虚拟目录:

如果我们想要一个虚拟目录添加到站点根目录映射到别的地方上,我们会做的文件系统:

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:\MySite\other_content");
serverManager.CommitChanges();

所得到的配置如下:

The resultant configuration looks like:

<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>

,我们认为这在IIS管理器:

And we see this in IIS Manager:

添加一个虚拟目录到现有的虚拟目录:

如果我们想要一个孩子虚拟目录添加到 vdir1 我们应该这样做:

If we wanted to add a child virtual directory to vdir1 we'd do:

root.VirtualDirectories.Add("/vdir_1/sub_dir1", @"d:\MySite\more_content");

这结果是:

<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管理器:

IIS Manager:

有几件事情要记住添加的虚拟目录时:

There's a couple things to keep in mind when adding virtual directories:


  • 如前所述,虚拟路径总是相对于父应用程序路径

  • 虚拟路径例如最后一部分 / vdir_1 ... / sub_dir1 将成为虚拟目录
  • 的名称
  • 这是完全合法的拥有多个虚拟目录指向相同的物理文件夹

  • 在一个虚拟目录路径的名称部分外,路径的所有部分的的存在,无论是作为物理路径或网站根目录下( D虚拟路径:\\ mysite的\\ WWW )。即路径应该能够覆盖的东西已经存在,否则在IIS管理器虚拟目录将不可见。

  • 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:\MySite\www). 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.

关于最后一点,例如,我们没有一个物理文件夹或叫虚拟目录 / vdir_2 但下面的code是完全合法的:

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:\MySite\even_more_content");

您不会看到 / vdir_2 / sub_dir1 在IIS管理器中显示出来,但它是合法的,你其实可以浏览到它。我们也可以看到它在的applicationHost.config

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:\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>

文件夹转换到应用程序:

如果你只是上传在网站ASP.NET应用程序的 / APP_1 文件夹,并且希望把它变成自己的应用程序,我们做到这一点:

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:\MySite\www\app_1");
// set application pool, otherwise it'll run in DefaultAppPool
app.ApplicationPoolName = "MySite";
serverManager.CommitChanges();    

的applicationHost.config 我们可以看到一个新的&lt;应用&GT; 元素被添加:

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:\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>

在IIS中,我们看到:

In IIS we see:

这是做相当于右键单击转换为应用程序。

This is the equivalent of doing right-click "Convert to Application".

添加到现有应用应用:

添加应用程序与现有应用程序的孩子是很简单的。假设我们想使 / APP_1 / sub_app_1 的子应用 / APP_1

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:

我们只会做的:

Application app = 
  site.Applications.Add("/app_1/sub_app_1", @"d:\mysite\www\app_1\sub_app_1");
app.ApplicationPoolName ="MySite";

所得到的配置是这样的:

The resultant configuration would look like:

<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>

在IIS:

虚拟目录添加到应用程序:

现在如果我们想要一个虚拟目录添加到这个应用程序,我们会做:

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:\MySite\other_content");

的applicationHost.config 我们可以看到一个新的&LT; virtualDirectory&GT; 元素被添加:

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:\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>

在IIS中,我们看到:

In IIS we see:

同样值得注意的是,虚拟路径 / vdir1 总是相对于包含应用程序的路径是很重要的。

Again it is important to note that the virtual path /vdir1 is always relative to the path of the containing application.

转换现有的虚拟目录应用程序:

如果我们想转换成我们刚刚创建的虚拟目录( / APP_1 / vdir1 )到应用程序?我们需要做这两个步骤:

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();    

得到的的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>
  <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>

在IIS管理器中我们可以看到:

In IIS Manager we see:

添加应用现有的虚拟目录:

如果我们想要一个应用程序添加到一个虚拟目录,会发生什么,这是如何工作的?在这个例子中,我们将应用程序添加到我们在前面创建的虚拟目录 / vdir_1 / sub_dir1

What happens if we want to add an application to a virtual directory, how does that work? In this example we'll add an 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 resultant config looks like:

<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>

和在IIS管理器中我们可以看到:

And in IIS manager we see:

将现有的子文件夹到应用程序:

作为最后一个例子,我们希望把 / other_apps / sub_app_1 到应用程序:

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

我们的code如下:

Application app = 
   site.Applications.Add("/other_apps/sub_app_1", @"d:\mysite\other_content");
app.ApplicationPoolName="MySite";
serverManager.CommitChanges();

得到的配置:

<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>

在IIS管理器:

希望这有助于解释站点,应用程序和虚拟目录之间的关系。

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

这篇关于使用ServerManager的创建中应用应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆