Compartir a través de


Cómo: Crear secciones de configuración personalizadas mediante IConfigurationSectionHandler

Actualización: noviembre 2007

Si lo desea, puede extender el conjunto estándar de opciones de configuración de ASP.NET con sus propios elementos XML de configuración. Para ello, debe crear su propio controlador de sección de configuración.

El controlador debe ser una clase de .NET Framework que implemente la interfaz System.Configuration.IConfigurationSectionHandler o la clase System.Configuration.ConfigurationSection.

Nota:

En este tema se utiliza la interfaz System.Configuration.IConfigurationSectionHandler, que se ha dejado de utilizar en la versión 2.0 de .NET Framework. Para obtener un ejemplo de uso de la clase System.Configuration.ConfigurationSection, vea Cómo: Crear secciones de configuración personalizadas mediante ConfigurationSection. Si utiliza los ejemplos de código siguientes, genérelos con la versión 1.0 ó 1.1 de .NET Framework.

El controlador de sección interpreta y procesa las opciones definidas en los elementos de configuración XML dentro de una parte específica de un archivo Web.config y devuelve el objeto de configuración correspondiente basado en las opciones de configuración. El objeto de configuración devuelto por la clase de controlador puede ser cualquier estructura de datos; no está limitado por ninguna clase de configuración básica o ningún formato de configuración. ASP.NET utiliza el objeto de configuración para leer y escribir en su elemento de configuración personalizado.

Para crear un controlador de sección de configuración personalizado

  1. Cree una clase pública que implemente la interfaz System.Configuration.IConfigurationSectionHandler, como se ilustra en el código siguiente.

    Imports System
    Imports System.Collections
    Imports System.Text
    Imports System.Configuration
    Imports System.Xml
    
    Namespace MyConfigSectionHandler
      Public Class MyHandler
        Implements IConfigurationSectionHandler
    
        Public Function Create( _
        ByVal parent As Object, ByVal configContext As Object, ByVal section As System.Xml.XmlNode) _
        As Object Implements System.Configuration.IConfigurationSectionHandler.Create
    
          Throw New System.Exception("The method is not implemented.")
    
        End Function
      End Class
    End Namespace
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Configuration;
    using System.Xml;
    
    namespace MyConfigSectionHandler
    {
      public class MyHandler : IConfigurationSectionHandler
      {
        #region IConfigurationSectionHandler Members
    
        object IConfigurationSectionHandler.Create(
            object parent, object configContext, XmlNode section)
        {
          throw new Exception("The method is not implemented.");
        }
    
        #endregion
      }
    }
    
  2. Agregue su propio código para realizar el trabajo de configuración que desee.

    Por ejemplo, puede reemplazar la línea throw new Exception("The method is not implemented."); con código que obtenga los nombres de atributo y los valores de la propiedad Attributes del parámetro section, y devolver un objeto de configuración personalizado. En el siguiente ejemplo se utiliza Hashtable como objeto de configuración.

    Imports System
    Imports System.Collections
    Imports System.Text
    Imports System.Configuration
    Imports System.Xml
    
    Namespace MyConfigSectionHandler
    
      Public Class MyHandler
        Implements IConfigurationSectionHandler
    
        Public Function Create( _
        ByVal parent As Object, ByVal configContext As Object, ByVal section As System.Xml.XmlNode) _
        As Object Implements System.Configuration.IConfigurationSectionHandler.Create
    
          ' Creates the configuration object that this method will return.
          ' This can be a custom configuration class.
          ' In this example, we use a System.Collections.Hashtable.
          Dim myConfigObject As New Hashtable
    
          ' Gets any attributes for this section element.
          Dim myAttribs As New Hashtable
          For Each attrib As XmlAttribute In section.Attributes
            If XmlNodeType.Attribute = attrib.NodeType Then
              myAttribs.Add(attrib.Name, attrib.Value)
            End If
          Next
    
          ' Puts the section name and attributes as the first config object item.
          myConfigObject.Add(section.Name, myAttribs)
    
          ' Gets the child element names and attributes.
          For Each child As XmlNode In section.ChildNodes
            If XmlNodeType.Element = child.NodeType Then
              Dim myChildAttribs As New Hashtable
    
              For Each childAttrib As XmlAttribute In child.Attributes
                If XmlNodeType.Attribute = childAttrib.NodeType Then
                  myChildAttribs.Add(childAttrib.Name, childAttrib.Value)
                End If
              Next
              myConfigObject.Add(child.Name, myChildAttribs)
            End If
          Next
    
          Return (myConfigObject)
    
        End Function
    
      End Class
    
    End Namespace
    
    using System;
    using System.Collections;
    using System.Text;
    using System.Configuration;
    using System.Xml;
    
    namespace MyConfigSectionHandler
    {
      public class MyHandler : IConfigurationSectionHandler
      {
        #region IConfigurationSectionHandler Members
    
        object IConfigurationSectionHandler.Create(
          object parent, object configContext, XmlNode section)
        {
          // Creates the configuration object that this method will return.
          // This can be a custom configuration class.
          // In this example, we use a System.Collections.Hashtable.
          Hashtable myConfigObject = new Hashtable();
    
          // Gets any attributes for this section element.
          Hashtable myAttribs = new Hashtable();
          foreach (XmlAttribute attrib in section.Attributes)
          {
            if (XmlNodeType.Attribute == attrib.NodeType)
              myAttribs.Add(attrib.Name, attrib.Value);
          }
    
          // Puts the section name and attributes as the first config object item.
          myConfigObject.Add(section.Name, myAttribs);
    
          // Gets the child element names and attributes.
          foreach (XmlNode child in section.ChildNodes)
          {
            if (XmlNodeType.Element == child.NodeType)
            {
              Hashtable myChildAttribs = new Hashtable();
    
              foreach (XmlAttribute childAttrib in child.Attributes)
              {
                if (XmlNodeType.Attribute == childAttrib.NodeType)
                  myChildAttribs.Add(childAttrib.Name, childAttrib.Value);
              }
              myConfigObject.Add(child.Name, myChildAttribs);
            }
          }
    
          return (myConfigObject);
        }
        #endregion
      }
    }
    

Para agregar un controlador de sección personalizado a un archivo de configuración de ASP.NET

  1. Agregue un elemento sectionGroup y section al archivo Web.config dentro del elemento configSections, como se ilustra en el código siguiente.

    Nota:

    El anidamiento de un elemento section en sectionGroup es opcional, pero se recomienda hacerlo para ayudar a organizar los datos de configuración.

    Puede agregar la declaración del controlador de sección en un archivo de configuración diferente del archivo en el que agrega los elementos de configuración personalizados, siempre y cuando el archivo de configuración donde se declare el controlador de sección esté más alto en la jerarquía de archivos de configuración. Para obtener más información, vea Jerarquía de archivos y herencia de la configuración de ASP.NET.

    El atributo type del elemento section debe coincidir con el manifiesto del ensamblado; de lo contrario, se producirá un error de configuración. El propio archivo de ensamblado debe estar en el mismo directorio de aplicación ASP.NET que el archivo Web.config que lo define.

    <configuration>
    
    <!-- Configuration section-handler declaration area. -->
      <configSections>
        <sectionGroup name="myCustomGroup">
          <section 
            name="myCustomSection" 
            type="MyConfigSectionHandler.MyHandler, MyCustomConfigurationHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" 
            allowLocation="true" 
            allowDefinition="Everywhere"
          />
        </sectionGroup>
          <!-- Other <section> and <sectionGroup> elements. -->
      </configSections>
    
      <!-- Configuration section settings area. -->
    
    </configuration>
    
  2. Agregue sus elementos de configuración personalizados en el área de la sección de configuración de su archivo Web.config.

    <configuration>
    
    <!-- Configuration section-handler declaration area. -->
    
      <!-- Configuration section settings area. -->
      <myCustomGroup>
        <myCustomSection myAttrib1="Clowns">
          <myChildSection 
              myChildAttrib1="Zippy" 
              myChildAttrib2="Michael Zawondy "/>
        </myCustomSection>
      </myCustomGroup>
    
      <!-- Other configuration settings, like <system.web> -->
    
    </configuration>
    

Para tener acceso mediante programación a sus datos de configuración personalizados

  • Obtenga una instancia de su objeto de configuración personalizado y utilice el método ConfigurationManager.GetSection o el método WebConfigurationManager.GetSection para rellenarlo.

    El ejemplo siguiente de una página ASPX funciona con los ejemplos anteriores para enumerar los atributos y elementos secundarios de la sección de configuración personalizada cuando se hace clic en un botón.

    Puesto que el controlador de sección personalizado utiliza Hashtable como objeto de configuración, el método GetSection devuelve Hashtable.

    El código siguiente se inserta en la página .aspx. El código correspondiente al evento click del botón, que se inserta en el archivo de código subyacente, se representa en el siguiente ejemplo de código.

    Los ejemplos de código siguientes requieren la versión 1.0 ó 1.1 de .NET Framework.

    <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="BugTest.WebForm1" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > 
    
    <html>
      <head>
        <title>WebForm1</title>
        <meta name="GENERATOR" 
              Content="Microsoft Visual Studio .NET 7.1">
        <meta name="CODE_LANGUAGE" Content="C#">
        <meta name=vs_defaultClientScript content="JavaScript">
        <meta name=vs_targetSchema 
              content="https://schemas.microsoft.com/intellisense/ie5">
      </head>
      <body MS_POSITIONING="GridLayout">
        <form id="Form1" method="post" >
        <h1>Enumerate MyCustomSection</h1>
        <asp:Label ID="Label1"  
            Text="" />
        <br />
        <asp:Button ID="Button1"  
            Text="Get Custom Config Info" 
            OnClick="Button1_Click" />
        </form>
      </body>
    </html>
    
    <%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="WebApplication1.WebForm1"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
      <HEAD>
        <title>WebForm1</title>
        <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
        <meta name="CODE_LANGUAGE" Content="C#">
        <meta name="vs_defaultClientScript" content="JavaScript">
        <meta name="vs_targetSchema" content="https://schemas.microsoft.com/intellisense/ie5">
      </HEAD>
      <body MS_POSITIONING="GridLayout">
        <form id="Form1" method="post" >
          <h1>Enumerate MyCustomSection</h1>
          <asp:Label ID="Label1"  Text="" />
          <br>
          <asp:Button ID="Button1"  Text="Get Custom Config Info" OnClick="Button1_Click" />
        </form>
      </body>
    </HTML>
    

    El código siguiente correspondiente al evento click del botón entra en el archivo de código subyacente que pertenece a la página .aspx anterior. El archivo de código subyacente utiliza la extensión de nombre de archivo .aspx.cs o .aspx.vb, dependiendo del atributo language de la declaración @Page.

    [C#]

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Configuration;
    using System.Text;
    
    namespace BugTest
    {
        /// <summary>
        /// Summary description for WebForm1.
        /// </summary>
        public class WebForm1 : System.Web.UI.Page
        {
            protected System.Web.UI.WebControls.Label Label1;
            protected System.Web.UI.WebControls.Button Button1;
    
            private void Page_Load(object sender, System.EventArgs e)
            {
                // Put user code to initialize the page here
            }
    
            #region Web Form Designer generated code
            override protected void OnInit(EventArgs e)
            {
                //
                // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                //
                InitializeComponent();
                base.OnInit(e);
            }
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {    
                this.Button1.Click += new System.EventHandler(this.Button1_Click);
                this.Load += new System.EventHandler(this.Page_Load);
    
            }
            #endregion
    
            protected void Button1_Click(object sender, System.EventArgs e)
            {
                Hashtable config = 
    (Hashtable)System.Configuration.ConfigurationSettings.GetConfig("myCustomGroup/myCustomSection");
    
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("Config object item count = {0}<br/><br/>", config.Count);
                foreach (DictionaryEntry deKey in config)
                {
                    sb.AppendFormat("<h2>Attributes in the {0} Element:</h2>", 
                        deKey.Key.ToString());
                    Hashtable attribs = (Hashtable)deKey.Value;
                    foreach (DictionaryEntry deAttrib in attribs)
                    {
                        sb.AppendFormat("{0} = {1}<br/>", 
                            deAttrib.Key.ToString(), deAttrib.Value.ToString());
                    }
                }
                Label1.Text = sb.ToString();
                Label1.Visible = true;
    
            }
        }
    }
    
    Imports System.Text
    
    Public Class WebForm1
      Inherits System.Web.UI.Page
    
    #Region " Web Form Designer Generated Code "
    
      'This call is required by the Web Form Designer.
      <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    
      End Sub
      Protected WithEvents Label1 As System.Web.UI.WebControls.Label
      Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    
      'NOTE: The following placeholder declaration is required by 
      ' the Web Form Designer.
      'Do not delete or move it.
      Private designerPlaceholderDeclaration As System.Object
    
      Private Sub Page_Init(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
      End Sub
    
    #End Region
    
      Private Sub Page_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
      End Sub
    
      Protected Sub Button1_Click(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles Button1.Click
        Dim config As Hashtable = _
    System.Configuration.ConfigurationSettings.GetConfig( _
    "myCustomGroup/myCustomSection")
    
        Dim sb As New StringBuilder
        sb.AppendFormat("Config object item count = {0}<br/><br/>", _
    config.Count)
    
        For Each deKey As DictionaryEntry In config
          sb.AppendFormat("<h2>Attributes in the {0} Element:</h2>", _
    deKey.Key.ToString())
          Dim attribs As Hashtable = deKey.Value
          For Each deAttrib As DictionaryEntry In attribs
            sb.AppendFormat("{0} = {1}<br/>", deAttrib.Key.ToString(), _
    deAttrib.Value.ToString())
          Next
          Label1.Text = sb.ToString()
          Label1.Visible = True
        Next
    
      End Sub
    End Class
    

Vea también

Tareas

Cómo: Crear secciones de configuración personalizadas mediante ConfigurationSection

Conceptos

Estructura de archivos de configuración de ASP.NET (secciones y controladores de sección)

Otros recursos

Administrar sitios web ASP.NET

Administrar sitios web ASP.NET

Configurar aplicaciones