Asp.net web.config appsettings multiple values

I have an appSetting block that looks like this:

 <appSettings> <key="site1" value="http://www.thissite.com,site name" /> <key="site2" value="http://www.thissite.com,site name" /> </appSettings> 

I want to populate a dropdown with values ​​and text:

value = "http://www.thissite.com" text = "site name"

I can get them in separate arrays using this:

 string[] mykey = ConfigurationManager.AppSettings["site1"].Split(','); string[] mykey = ConfigurationManager.AppSettings["site2"].Split(','); 

however, I want to combine them into a single array, and then scroll and populate the code drop-down list. I can fill it this way by going through separate arrays, but it seems like there should be a better way with less code.

Can anyone tell me how?


credit to you all, except for the gratitude to acermate433s below.

 NameValueCollection appSettings = ConfigurationManager.AppSettings; for (int i = 0; i < appSettings.Count; i++) { Response.Write(appSettings.GetKey(i).ToString() + "-" + appSettings[i].ToString()); } 

Obviously, I will do a little more than just show it.

+7
source share
3 answers

AppSettings is a NameValueCollection , you can scroll through all its values, using for each

+7
source

You can expand the configuration file by creating custom configurations. Essentially you get:

 <site name="key1"> <address value="...1..." /> </site> 

http://www.4guysfromrolla.com/articles/020707-1.aspx

Alternatively, you can specify the key as the site name and simply use http://cephas.net/blog/2003/09/26/extending-webconfig-in-aspnet/ .

+2
source

Just to give a complete working sample as this question continues to get hits

 NameValueCollection appSettings = ConfigurationManager.AppSettings; for (int i = 0; i < appSettings.Count; i++) { string key = appSettings.GetKey(i); string value = appSettings.Get(i); } 
+2
source

All Articles