2011-02-17 28 views
19

no encuentro ningún ejemplo de cómo acceder a una sección de configuración tales anidada en un app.configanidada Sección de configuración App.config

<my.configuration> 
    <emailNotification> 
     <to value="[email protected]" /> 
     <from value="[email protected]" /> 
     <subject value="Subject" /> 
     <smtpHost value="smtp.you.com" /> 
     <triggers> 
     <add name="1" varAlias="Var1" lower="-200" upper="-150"/> 
     </triggers> 
    </emailNotification> 
    </my.configuration> 

Solía ​​ConfigurationElementCollection y ConfigurationElement antes. Pero no sé cómo hacer lo anterior?

+0

supongo que el 'my.configuration' es un grupo y' emailNotification' es el anidado sección a la que se refiere. ¿Correcto? –

+0

my.configuration es una sección

Y emailNotification the group, yes – chriszero

+0

Voy a publicar un ejemplo en un minuto. –

Respuesta

35

es necesario:

Definir my.configuration como grupo sección y emailNotification como una sección dentro del grupo. Añadir según el fichero de configuración:

<configSections> 
    <sectionGroup name="my.configuration" 
        type="SectionGroupRetrieval.MyConfigurationGroup, SectionGroupRetrieval"> 
     <section name="emailNotification" 
       type="SectionGroupRetrieval.EmailNotificationSection, SectionGroupRetrieval" /> 
    </sectionGroup>  
</configSections> 

implementar el grupo de secciones de configuración (my.configuration).

public class MyConfigurationGroup : ConfigurationSectionGroup 
{ 
    [ConfigurationProperty("emailNotification")] 
    public EmailNotificationSection EmailNotification 
    { 
     get { return (EmailNotificationSection)base.Sections[ "emailNotification" ]; } 
    } 
} 

implementan la sección de configuración (emailNotification).

public class EmailNotificationSection : ConfigurationSection 
{ 
    [ConfigurationProperty("to")] 
    public ValueElement To 
    { 
     get { return (ValueElement)base[ "to" ]; } 
    } 

    [ConfigurationProperty("from")] 
    public ValueElement From 
    { 
     get { return (ValueElement)base[ "from" ]; } 
    } 

    [ConfigurationProperty("subject")] 
    public ValueElement Subject 
    { 
     get { return (ValueElement)base[ "subject" ]; } 
    } 

    [ConfigurationProperty("smtpHost")] 
    public ValueElement SmtpHost 
    { 
     get { return (ValueElement)base[ "smtpHost" ]; } 
    } 

    [ConfigurationProperty("triggers")] 
    public TriggerElementCollection Triggers 
    { 
     get { return (TriggerElementCollection)base[ "triggers" ]; } 
    } 
} 

Implementa los elementos de configuración necesarios y la colección de elementos de configuración.

public class ValueElement : ConfigurationElement 
{ 
    [ConfigurationProperty("value")] 
    public string Value 
    { 
     get { return (string)base[ "value" ]; } 
     set { base[ "value" ] = value; } 
    } 
} 

public class TriggerElement : ConfigurationElement 
{ 
    [ConfigurationProperty("name")] 
    public string Name 
    { 
     get { return (string)base[ "name" ]; } 
     set { base[ "name" ] = value; } 
    } 

    [ConfigurationProperty("varAlias")] 
    public string VarAlias 
    { 
     get { return (string)base[ "varAlias" ]; } 
     set { base[ "varAlias" ] = value; } 
    } 

    [ConfigurationProperty("lower")] 
    public int Lower 
    { 
     get { return (int)base[ "lower" ]; } 
     set { base[ "lower" ] = value; } 
    } 

    [ConfigurationProperty("upper")] 
    public int Upper 
    { 
     get { return (int)base[ "upper" ]; } 
     set { base[ "upper" ] = value; } 
    } 
} 

[ConfigurationCollection(typeof(TriggerElement))] 
public class TriggerElementCollection : ConfigurationElementCollection 
{ 
    public TriggerElement this[ string name ] 
    { 
     get { return (TriggerElement)base.BaseGet(name); } 
    } 

    public TriggerElement this[ int index ] 
    { 
     get { return (TriggerElement)base.BaseGet(index); } 
    } 

    protected override ConfigurationElement CreateNewElement() 
    { 
     return new TriggerElement(); 
    } 

    protected override object GetElementKey(ConfigurationElement element) 
    { 
     return ((TriggerElement)element).Name; 
    } 
} 

Después de actualizar el archivo de configuración y la implementación de bits de configuración necesarios, puede acceder a que la sección de la siguiente manera:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
MyConfigurationGroup myConfiguration = (MyConfigurationGroup)config.GetSectionGroup("my.configuration"); 
EmailNotificationSection section = myConfiguration.EmailNotification; 
+0

Esto también funciona para ASP.NET MVC 4.5 cuando se usa Configuración de configuración = WebConfigurationManager.OpenWebConfiguration (HttpContext.Request.ApplicationPath); – Ako

Cuestiones relacionadas