获取 IIS 7 站点属性
我有一个 C++ 应用程序,需要检索 IIS 7 站点的属性(例如类似于 IIS6 中的元数据库属性 - Path
、AppFriendlyName
等)。
在 IIS 7 中,我的代码执行以下操作:
- 获取
AppHostWritableAdminManager
并提交路径MACHINE/WEBROOT/APPHOST/Default Web Site/
。 - 使用部分名称
appSettings
调用GetAdminSection
。 - 然后查看返回的集合并查找属性(例如
Path
)。
这适用于 IIS 6,但不适用于 IIS7/7.5。
为了使这项工作顺利进行,我需要做出哪些改变?
I have a C++ application that needs to retrieve an IIS 7 site's properties (such as metabase properties similar to those in IIS6 - Path
, AppFriendlyName
etc).
With IIS 7, my code does this:
- Get the
AppHostWritableAdminManager
and commit the pathMACHINE/WEBROOT/APPHOST/Default Web Site/
. - Call
GetAdminSection
with the section nameappSettings
. - Then look at the returned collection and look for the property (
Path
for example).
This works in IIS 6 but not on IIS7/7.5.
What changes do I need to make in order to make this work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 IIS7 中,配置数据并不存储在“元数据库”中,而且我们在 IIS6 中习惯的元数据库属性也不同。 IIS7 将其大部分配置数据存储在以下文件中:
还有其他文件,但为了回答这个问题,这是我们感兴趣的文件。
的文档applicationHost.config 可以在这里找到:
您可以找到 IIS6 元数据库列表 -> IIS7 XML 配置映射如下:
例如,在 IIS6 中,站点
/root
的路径存储在IIsWebVirtualDir
的Path
属性中。即:但在 IIS7 中,它的存储方式有所不同:
但是,如果您的代码必须同时适用于 IIS6 和 IIS7,那么您可以安装 IIS6 管理兼容性组件。这将允许您使用传统的 IIS6 元数据库 API(例如 ADSI、System.DirectoryServices 等)访问 IIS7 站点属性。兼容性层将为您将这些属性映射到新的 IIS7 架构。
本文的第一部分解释了如何在 Vista/Windows7/Windows 2008 上安装它:
更新:
不幸的是,C++ 不是我的强项。不过,我使用 COM Interop 在 C# 中组合了一个示例,以演示如何使用
AppHostWritableAdminManager
:上面的代码在
集合中定位名为“MySite”的站点。然后,它检索站点的
集合并打印每个绑定的protocol
和bindingInformation
属性。您应该能够相当轻松地将其转换为 C++。
回答您评论中的问题 -
使用
AppHostWritableAdminManager
时,没有直接访问您想要检查/修改的网站或其属性的快捷方式。在上面的示例中,您将看到我需要循环访问网站集以找到我感兴趣的网站。原因是AppHostWritableAdminManager
将所有内容视为元素和元素集合。这是一个相当基本的 API。即使使用托管Microsoft.Web.Administration
API,您也会发现虽然有一些不错的属性,例如Site.Bindings
,但它们只是对AppHostWritableAdminManager 进行了轻微伪装的包装器
。事实上,如果我想找到一个站点,我仍然需要在 for 循环中搜索
Sites
集合,或者如果我使用 C#3.5 或更高版本,则需要添加一些 LINQ 糖:Site
的基类是ConfigurationElement
,它在引擎盖下封装了对IAppHostElement
的访问。一旦您了解了一些基本的快捷方式包装器属性,我们在托管代码中配置 IIS(例如 IIS FTP)的大部分工作就是元素、属性和元素集合。
更新 2:
请记住,我一生中从未写过一行 C++ 代码。没有清理字符串或对象:
In IIS7 the configuration data is not stored in a "metabase" and also the metabase properties that we're accustomed to in IIS6 aren't the same. IIS7 stores the bulk of its configuration data in the following file:
There are other files, but for the purposes of answering this question, this is the file we're interested in.
The documentation for
applicationHost.config
can be found here:You can find a list of IIS6 metabase -> IIS7 XML configuration mappings here:
For example in IIS6 the path to a site's
/root
is stored in thePath
attribute ofIIsWebVirtualDir
. i.e.:But in IIS7 it's stored differently:
However, if your code must work with both IIS6 and IIS7 then you can install the IIS6 Management Compatibility components. This will allow you to access IIS7 site properties using traditional IIS6 metabase API's such as ADSI, System.DirectoryServices etc. The compatibility layer will map these properties to the new IIS7 schema for you.
The first part of this article explains how to install this on Vista/Windows7/Windows 2008:
Update:
Unfortunately C++ isn't my strong point. However I put together an example in C# using COM Interop to demonstrate using the
AppHostWritableAdminManager
:The code above locates a site named "MySite" in the
<sites>
collection. It then retrieves the site's<bindings>
collection and print's each bindingsprotocol
andbindingInformation
attributes.Your should be able to convert this to C++ fairly easily.
To answer the question in your comment -
When using the
AppHostWritableAdminManager
there isn't a shortcut to getting directly to the site you want to inspect/modify or it's properties. In the example above, you'll see that I need to loop through the sites collection to find the site I'm interested in. The reason for this is thatAppHostWritableAdminManager
sees everything as elements and collections of elements. It's a fairly basic API. Even when using the managedMicrosoft.Web.Administration
API you find that whilst there are some nice properties such asSite.Bindings
, these are thinly disguised wrappers aroundAppHostWritableAdminManager
.In fact if I want to find a site I still have to search the
Sites
collection either in a for loop or by adding some LINQ sugar if I'm using C#3.5 or later:Site
's base class isConfigurationElement
which under the bonnet wraps access toIAppHostElement
.Once you're past some basic shortcut wrapper properties much of what we do in managed code to configure IIS (for example IIS FTP) is elements, attributes and collections of elements.
Update 2:
Please bear in mind I've never written a line of C++ in my life. There's no cleanup of strings or objects: