How to iterate through user keys in web.config?

Is it possible to create my own custom keys in the asp.net web.config file and iterate through C #? How do you do both (where can I put the key? What format?)? I have an intranet application that does certain things based on the client IP address. Instead of hard coding in the codebehind file, I thought I would put them in web.config and repeat this. That way, I could add or remove from my configuration file without recompiling everything.

My key will have a name, IP address, and possibly other information.

Thank.

+3
source share
3 answers

I think this should do it for you ...

This is in your web.config ...

<configSections>
    <section name="DataBaseKeys" type="System.Configuration.NameValueFileSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</configSections>
<DataBaseKeys>
    <!--Connection Strings for databases (or IP Addresses or whatever)-->
    <add key="dbCon1" value="Data Source=DbServerPath;Integrated Security=True;database=DbName1"/>
    <add key="dbCon2" value="Data Source=DbServerPath;Integrated Security=True;database=DbName1"/>
    <add key="dbCon3" value="Data Source=DbServerPath;Integrated Security=True;database=DbName1"/>
    <add key="dbCon4" value="Data Source=DbServerPath;Integrated Security=True;database=DbName1"/>
    <add key="dbCon5" value="Data Source=DbServerPath;Integrated Security=True;database=DbName1"/>  
</DataBaseKeys>

This is your code ...

using System.Configuration;

using System.Collections.Specialized;

protected void Page_Load(object sender, EventArgs e)
{
    LoadDdls();
}

private void LoadDdls()
{
    NameValueCollection nvcDbKeys = GetDbKeys();

    //Loop through the collection       
    for (int i = 0; i < nvcDbKeys.Count; i++)
    {
        // "Keys" is the "key" - Get(int) is the "value"
        this.DropDownList1.Items.Add(new ListItem(nvcDbKeys.Keys[i], nvcDbKeys.Get(i)));
    }
}

private NameValueCollection GetDbKeys()
{
    //Declare a name value collection to store Database Key List from web.config
    NameValueCollection nvcDatabaseKeyList;
    nvcDatabaseKeyList = (NameValueCollection) ConfigurationManager.GetSection("DataBaseKeys");

    return nvcDatabaseKeyList;
}
+10

. , umm, web.config , .

+2

. appSettings , .. "key1", "key2" .., , , . , .. "1; 2; value3;..".

. , - web.config. web.config .

<configuration>
   <configSections>
      <sectionGroup name="MySectionGroup">
          <section name="MySection" type="[type and full assembly name]"/>

   ...
   <MySectionGroup>
      <MySection>
         [some xml]

Next, create a section handler class; it needs to implement the IConfigurationSectionHandler interface, which defines the Create method. Create takes sectionNode as a parameter, which is an XML node that you can parse as you want. The return object must contain the data that you analyzed. To load the section handler, write:

MySectionDataObject myData = ConfigurationManager.GetSection( "MySectionGroup/Section" ) as MySectionDataObject
+2
source

All Articles