新しい構成セクションの作成

独自の XML 構成タグを使用して、標準の ASP.NET 構成設定のセットを拡張できます。拡張する場合は、独自の構成セクション ハンドラを作成する必要があります。このハンドラは、IConfigurationSectionHandler インターフェイスを実装した .NET Framework クラスである必要があります。セクション ハンドラは、Web.config ファイルの特定の部分にある XML タグで定義された設定を解釈して処理し、その構成設定に基づいて適切な構成オブジェクトを返します。ハンドラ クラスが返す構成オブジェクトでは、任意のデータ構造を使用できます。このオブジェクトは基本構成クラスや構成形式に限定されません。

IConfigurationSectionHandler インターフェイスを定義する例を次に示します。

Namespace System.Web.Configuration 
   Public Interface IConfigurationSectionHandler 
      Function Create(parent As Object, input As Object, _
         node As XmlNode) As Object
   End Interface
End Namespace
[C#]
namespace System.Web.Configuration 
{
   public interface IConfigurationSectionHandler 
   {
      public Object Create(Object parent, Object input, 
         XmlNode node);
   }
}

<appSettings> セクションと同じ構成ハンドラを使用する、独自のセクションを定義することもできます。次に例を示します。

<configuration>
   <configSections>
      <sectionGroup name="myGroup">
         <sectionGroup name="nestedGroup">
            <section name="mySection" type=
               "System.Configuration.NameValueSectionHandler,System"/>
         </sectionGroup>
      </sectionGroup>
   </configSections>

   <myGroup>
      <nestedGroup>
         <mySection>
            <add key="key_one" value="1"/>
            <add key="key_two" value="2"/>
         </mySection>
      </nestedGroup>
   </myGroup>
</configuration>

上の例で定義した新しい構成セクションの値を読み込むには、次のようにします。

Dim config As NameValueCollection =      
   ConfigurationSettings.GetConfig("myGroup/nestedGroup/mySection")
Response.Write("The value of key_one is " & Server.HtmlEncode(config("key_one")) & "<br>")
Response.Write("The value of key_two is " & Server.HtmlEncode(config("key_two")) & "<br>")
[C#]
NameValueCollection config = (NameValueCollection)
   ConfigurationSettings.GetConfig("myGroup/nestedGroup/mySection");
Response.Write("The value of key_one is " + Server.HtmlEncode(config["key_one"]) + "<br>");
Response.Write("The value of key_two is " + Server.HtmlEncode(config["key_two"]) + "<br>");

参照

ASP.NET の構成 | アプリケーションの設定