简体   繁体   English

如何将IIS ApplicationHost.config文件解析为.Net对象?

[英]How to parse the IIS ApplicationHost.config file into .Net objects?

I am writing an app to interrogate what sites are active on an IIS server. 我正在编写一个应用程序来询问IIS服务器上哪些站点处于活动状态。 The best way seems to be to directly read the ApplicationHost.config file. 最好的方法似乎是直接读取ApplicationHost.config文件。

What's the easiest way to go about this? 最简单的方法是什么?

Is it possible to write back a new site using this method also? 是否也可以使用此方法写回新站点?

1) ApplicationHost.config is an XML file so you can simply use the XDocument class. 1)ApplicationHost.config是一个XML文件,因此您可以简单地使用XDocument类。

2) Yes - XDocument allows construction of new XML elements. 2)是-XDocument允许构造新的XML元素。

Since the ApplicationHost.Config is just an XML, you could use some tool to generate classes from it, something like Xml2CSharp . 由于ApplicationHost.Config只是XML,因此您可以使用一些工具从中生成类,例如Xml2CSharp You may need to adapt one thing or another, but it's a start. 您可能需要适应一件事或另一件事,但这是一个开始。

Then you deserialize the ApplicationHost.config into objects like a regular XML. 然后,将ApplicationHost.config反序列化为类似于常规XML的对象。

You could directly change the ApplicationHost.config to add sites, application pools and change other configs, but I wouldn't recommend to do so. 您可以直接更改ApplicationHost.config来添加站点,应用程序池并更改其他配置,但是我不建议这样做。

You can use the ServerManager class. 您可以使用ServerManager类。 Here you are a very basic sample, so you can start playing around: 这里是一个非常基本的示例,因此您可以开始玩:

using(var serverManager = ServerManager.OpenRemote("my-remote-server"))
{
    if (serverManager.Sites.AllowsAdd())
    {
        var site = serverManager.Sites.Add(siteName, path, port);
        // use site for something, like changing its bindings or something else
        serverManager.CommitChanges(); // without this, changes are made only in memory
    }
}

Make sure you the application which will execute this code runs under a user which exists in the remote server. 确保将执行此代码的应用程序在远程服务器中存在的用户下运行。 The application needs to run in a machine and user account which can remotely access the target IIS server. 该应用程序需要在可以远程访问目标IIS服务器的计算机和用户帐户中运行。

Also, you need to add a reference to Microsoft.Web.Administration , should be located under c:\\windows\\system32\\inetsrv. 另外,您需要添加对Microsoft.Web.Administration的引用,该引用应位于c:\\ windows \\ system32 \\ inetsrv下。

I generated classes with Xml2CSharp with the applicationHost.config file I had. 我使用Xml2CSharp和我拥有的applicationHost.config文件生成了类。 I also was only interested in bindings per site. 我也只对每个站点的绑定感兴趣。 The main changes were to correct class and variable names with full stops (system.webServer -> systemwebServer). 主要更改是使用句号(system.webServer-> systemwebServer)纠正类和变量名。 Stackoverflow won't host uploaded class files and at 815 lines the generated class was a bit long to post. Stackoverflow不会托管上载的类文件,并且在815行生成的类要发布的时间有点长。 However to get started this may help 但是,这可能会有所帮助

    JavaScriptSerializer ser = new JavaScriptSerializer();

string applicationHostFile = @"C:\Windows\System32\inetsrv\Config\applicationHost.config";

using (System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader(applicationHostFile))
{
    System.Xml.Serialization.XmlSerializer DeSerializer = new System.Xml.Serialization.XmlSerializer(typeof(Xml2CSharp.Configuration));
    Xml2CSharp.Configuration configuration = (Xml2CSharp.Configuration)DeSerializer.Deserialize(reader);

    foreach (var site in configuration.SystemapplicationHost.Sites.Site)
    {
        // clear Application to not expose sensitive information
        site.Application = null;
        Console.WriteLine(site.Name);
        foreach (var binding in site.Bindings.Binding)
        {
            Console.WriteLine(binding.BindingInformation);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Applicationhost.config未更新 - Applicationhost.config not updated 初始化 applicationhost.config 文件失败。 找不到 IIS Express - Initializing the applicationhost.config file failed. Cannot find IIS Express applicationHost.config 错误:由于 IIS 共享配置的权限不足,无法写入配置文件 - applicationHost.config Error: Cannot write configuration file due to insufficient permissions with IIS shared configuration IIS CLI为我的项目生成带有site的applicationhost.config - IIS CLI generate applicationhost.config with site for my project 由于applicationHost.config无效,.Net构建失败 - .Net build is failing due to invalid applicationHost.config 即使修改后也无法从 .NET Core 本地服务器提供某些文件扩展名<requestFiltering>在 applicationHost.config 中 - Can't serve some file extensions from .NET Core local server even after modifying <requestFiltering> in applicationHost.config 我是否必须手动编辑 applicationhost.config 以将特定绑定添加到 IIS Express - Must I manually edit the applicationhost.config to add specific bindings to IIS Express 以编程方式解锁applicationHost.config中的部分 - Unlock section in applicationHost.config programmatically applicationhost.config中的位置路径未映射到程序集 - Location path in applicationhost.config not mapping to assembly C# applicationHost.config 未找到但存在 - C# applicationHost.config not found but exists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM