如何从 C# 为 IIS 服务器上的所有网站在 ApplicationHost.config 中添加 HttpModule?
How can I add a HttpModule in ApplicationHost.config for all websites on IIS Server from C#?
我正在尝试
public string[] RegisterInApplicationConfig()
{
using (ServerManager serverManager = new ServerManager())
{
Configuration config = serverManager.GetApplicationHostConfiguration();
var section = config.GetSection("location/system.webServer/modules");
}
}
但我得到的错误是 -
无法读取配置部分 location/system.webServer/modules
,因为它缺少部分声明。
我指的是 post 添加 HttpModule -
所以基本上在 ApplicationHostConfig 中我需要到达
<location path="" overrideMode="Allow">
<system.webServer>
<modules>
<add name="IsapiFilterModule" lockItem="true" />
所以我在经过一些尝试后找到了解决方案。将来可能会帮助某人-
我需要在“system.webServer/modules
”部分做 GetCollection
Configuration config = serverManager.GetApplicationHostConfiguration();
var section = config.GetSection("system.webServer/modules", "");
var collection = section.GetCollection();
var element = collection.CreateElement();
element.Attributes["name"].Value = "MyHttpModule";
element.Attributes["type"].Value = MyHttpModule.XXXModule, MyHttpModule, Version=1.0.0.0, Culture=neutral, PublicKeyToken=bc88fbd2dc8888795";
collection.Add(element);
serverManager.CommitChanges();
我正在尝试
public string[] RegisterInApplicationConfig()
{
using (ServerManager serverManager = new ServerManager())
{
Configuration config = serverManager.GetApplicationHostConfiguration();
var section = config.GetSection("location/system.webServer/modules");
}
}
但我得到的错误是 -
无法读取配置部分 location/system.webServer/modules
,因为它缺少部分声明。
我指的是 post 添加 HttpModule -
所以基本上在 ApplicationHostConfig 中我需要到达
<location path="" overrideMode="Allow">
<system.webServer>
<modules>
<add name="IsapiFilterModule" lockItem="true" />
所以我在经过一些尝试后找到了解决方案。将来可能会帮助某人-
我需要在“system.webServer/modules
”部分做 GetCollection
Configuration config = serverManager.GetApplicationHostConfiguration();
var section = config.GetSection("system.webServer/modules", "");
var collection = section.GetCollection();
var element = collection.CreateElement();
element.Attributes["name"].Value = "MyHttpModule";
element.Attributes["type"].Value = MyHttpModule.XXXModule, MyHttpModule, Version=1.0.0.0, Culture=neutral, PublicKeyToken=bc88fbd2dc8888795";
collection.Add(element);
serverManager.CommitChanges();