【问题标题】:How can I add a HttpModule in ApplicationHost.config for all websites on IIS Server from C#?如何从 C# 为 IIS 服务器上的所有网站在 ApplicationHost.config 中添加 HttpModule?
【发布时间】:2016-11-29 11:24:41
【问题描述】:

我正在尝试类似的东西

  public string[] RegisterInApplicationConfig()
    {
        using (ServerManager serverManager = new ServerManager())
        {
            Configuration config = serverManager.GetApplicationHostConfiguration();
            var section = config.GetSection("location/system.webServer/modules");

        }
    }

但我得到的错误是 -

无法读取配置节location/system.webServer/modules,因为它缺少节声明。

我指的是添加 HttpModule 的帖子 -

How do I register a HttpModule in the machine.config for IIS 7?

所以基本上我需要在 ApplicationHostConfig 中访问

<location path="" overrideMode="Allow">
    <system.webServer>

    <modules>
        <add name="IsapiFilterModule" lockItem="true" />

【问题讨论】:

    标签: c# asp.net iis ihttpmodule applicationhost


    【解决方案1】:

    所以我在经过一些尝试后找到了解决方案。将来可能会帮助某人-

    我需要在“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();
    

    【讨论】:

      猜你喜欢
      • 2010-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-03
      • 1970-01-01
      • 1970-01-01
      • 2013-05-08
      相关资源
      最近更新 更多