使用 ServerManager 在应用程序中创建应用程序 [英] Using ServerManager to create Application within Application

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

问题描述

我正在使用 ServerManager (Microsoft.Web.Administration.dll) 在 IIS 7 上的网站内创建应用程序.我需要在这个应用程序中创建一个应用程序或将一个虚拟目录转换为一个应用程序(如iis管理器功能右键单击->转换为应用程序)这如何可行?我发现关于这个库的文档很少,而且没有一个提到这个特定的功能.谢谢.

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:mysitewww.该站点的 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: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

首先,我们获取要添加应用程序的站点,我们将始终使用变量 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);

根/"应用程序:

每个站点都有一个根"应用程序.如果我们打开位于 %systemroot%windowssystem32inetsrvconfig 中的 applicationHost.config 并找到我们的 节点我们看到的网站如下:

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, /.

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

The applicationPool attribute specifies which application pool to use.

请注意,只有一个子元素:virtualDirectory.

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

每个 application 都有一个 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:

  • 这是根 (path="/") 和
  • 它在物理上位于 d:MySitewww (physicalPath="d:MySitewww") 的文件系统上.
  • this this is the root (path="/") and
  • that it's physically located on the file system at d:MySitewww (physicalPath="d:MySitewww").

每个virtualDirectorypath 相对于父application 路径中指定的path.

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

结果配置如下:

<site name="MySite" id="3">
  <application path="/" applicationPool="MySite">
    <virtualDirectory path="/" physicalPath="D:MySitewww" />
    <virtualDirectory path="/vdir_1" physicalPath="D:MySiteother_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:MySitemore_content");

这导致:

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

添加虚拟目录时需要注意以下几点:

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

  • 如前所述,虚拟path 总是相对于父应用程序path
  • 虚拟path的最后一部分,例如/vdir_1.../sub_dir1 成为虚拟目录的名称
  • 让多个虚拟目录指向同一个物理文件夹是完全合法的
  • 除了虚拟目录路径的名称部分之外,路径的所有部分应该作为物理路径或网站根目录中的虚拟路径存在 (d:MySitewww).即 path 应该能够覆盖已经存在的内容,否则虚拟目录在 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: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.

关于最后一点,例如,我们没有名为 /vdir_2 的物理文件夹或虚拟目录,但以下代码是完全合法的:

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

您不会看到 /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: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>

将文件夹转换为应用程序:

如果您刚刚将 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:MySitewwwapp_1");
// set application pool, otherwise it'll run in DefaultAppPool
app.ApplicationPoolName = "MySite";
serverManager.CommitChanges();    

applicationHost.config 中,我们可以看到添加了一个新的 元素:

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>

在 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:mysitewwwapp_1sub_app_1");
app.ApplicationPoolName ="MySite";

最终的配置看起来像:

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

在 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:MySiteother_content");

applicationHost.config 中我们可以看到添加了一个新的 元素:

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>

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

在 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:mysiteother_content");
app.ApplicationPoolName = "MySite";
serverManager.CommitChanges();

结果配置如下:

<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>
  <application path="/vdir_1/sub_dir1/app_2" applicationPool="MySite">
    <virtualDirectory path="/" physicalPath="d:mysiteother_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:

我们的代码如下:

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

结果配置:

<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>
  <application path="/vdir_1/sub_dir1/app_2" applicationPool="MySite">
    <virtualDirectory path="/" physicalPath="d:mysiteother_content" />
  </application>
  <application path="/other_apps/sub_app_1" applicationPool="MySite">
    <virtualDirectory path="/" physicalPath="d:mysiteother_content" />
  </application>
</site>

在 IIS 管理器中:

In IIS manager:

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

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

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

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