views:

1350

answers:

5

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

My key would have a name, IP address, and maybe other information.

Thank you.

A: 

I can't really understand what you're trying to do. Are you suggesting you want to store info in web.config which would be indexed by IP? This is bad. Not only would it restart the application each time, but the config is not used for storing such info - only for settings that change rarely.

It might be acceptable to change the config often in a win app, but not on a web app. You should make up a different solution. I this is not what you want, please explain a little more.

Slavo
A: 

There is a set of IP addresses that are used in my application. However, due to a network change or the addition of a new client, I might want to change one of them or add to the list or remove one from the list. If I keep them in the web.config file it is a simple text file change. They would not change that often. I don't want to store them in the codebehind because that makes even less sense to me.

johnny
+2  A: 

You can create a Custom Configuration Section. That allows you to put your, umm, custom config in web.config, and access it in whichever way you see fit.

Arnout
+1  A: 

Quick-and-dirty solution: Add your keys to appSettings with an index as postfix, ie. "key1", "key2", etc., and loop until you get to a key that doesn't exist. Or add a delimited list to a single key, ie. "value1;value2;value3;..".

Better solution: Create your own custom section handler, then you can add your data in your own way in a separate section of web.config. You'll need to define the section and sectiongroup at the top of web.config, with a reference to the section handler class.

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

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

Next create the section handler class, it needs to implement the interface IConfigurationSectionHandler, which defines a Create method. Create takes sectionNode as a parameter, which is an XML node you can parse in whatever way you want. The return object should contain the data you've parsed. To load the section handler, write:

MySectionDataObject myData = ConfigurationManager.GetSection( "MySectionGroup/Section" ) as MySectionDataObject
Bjørn Stærk
+1  A: 

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