2016-11-29 6 views
1

私はC#からIISサーバー上のすべてのWebサイトに対してApplicationHost.configにHttpModuleを追加するにはどうすればよいですか?

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をを追加してから記事を参照しています - ApplicationHostConfigにおけるだから、基本的に

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

私はいくつかの&トライアルを打った後、だから私は解決策を見つけた

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

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

答えて

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(); 
関連する問題