2011-02-17 4 views
19

Je ne trouve pas d'exemples de la façon d'accéder à une telle section de configuration imbriquée dans un app.configConfiguration Section Nested 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> 

je ConfigurationElementCollection et ConfigurationElement avant. Mais je ne sais pas comment faire ce qui précède?

+0

Je suppose que le 'my.configuration' est un groupe et' emailNotification' est imbriquée section dont vous parlez. Correct? –

+0

my.configuration est une section

Et emailNotification du groupe, oui – chriszero

+0

Je vais poster un exemple dans une minute. –

Répondre

35

Vous devez:

Définir my.configuration comme groupe de sections et emailNotification comme une section au sein du groupe. Ajouter suivante dans le fichier de configuration:

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

Mettre en oeuvre le groupe de section de configuration (my.configuration).

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

Implémentez la section de configuration (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" ]; } 
    } 
} 

Implémenter les éléments de configuration et la collection d'éléments de configuration nécessaires.

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; 
    } 
} 

Après la mise à jour du fichier de configuration et la mise en œuvre des bits de configuration nécessaires, vous pouvez vous accéder à la section comme suit:

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

Cela fonctionne aussi bien pour ASP.NET MVC 4.5 lors de l'utilisation Configuration configuration = WebConfigurationManager.OpenWebConfiguration (HttpContext.Request.ApplicationPath); – Ako

Questions connexes