asp.net web.config appsettings多个值

时间:2022-03-19 03:25:14

I have appSetting block that looks like this:

我有appSetting块,看起来像这样:

<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 list with the values and text:

我想用值和文本填充下拉列表:

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

value =“http://www.thissite.com”text =“网站名称”

I can get them into individual arrays using this:

我可以使用它将它们分成单个数组:

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

however, I want to combine them into one array and then loop through and populate the dropdown in the codebehind. I can populate it this way looping through the individual arrays, but it just seems as if there must be a better way with less code.

但是,我想将它们组合成一个数组然后循环并填充代码隐藏中的下拉列表。我可以通过循环遍历各个数组来填充它,但看起来好像必须有更好的方法来减少代码。

Can anyone tell me how?

谁能告诉我怎么样?


credit to you all but many thanks to acermate433s' answer below.

很遗憾,感谢acermate433s的回答。

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 bit more than just display it.

显然,我会做的不仅仅是显示它。

3 个解决方案

#1


7  

AppSettings is a NameValueCollection, you could loop through all of its values using for each

AppSettings是一个NameValueCollection,您可以使用每个值循环遍历其所有值

#2


2  

You can extend the config file by creating custom configurations. Essentially you will end up with :

您可以通过创建自定义配置来扩展配置文件。基本上你最终会得到:

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

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

Alternatively, you could specify the key as the name of the site and just use http://cephas.net/blog/2003/09/26/extending-webconfig-in-aspnet/ sort of thing.

或者,您可以将密钥指定为站点的名称,只需使用http://cephas.net/blog/2003/09/26/extending-webconfig-in-aspnet/。

#3


2  

Just to give full working sample as this question keeps getting hits

只是为了提供完整的工作样本,因为这个问题不断受到欢迎

NameValueCollection appSettings = ConfigurationManager.AppSettings;

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

#1


7  

AppSettings is a NameValueCollection, you could loop through all of its values using for each

AppSettings是一个NameValueCollection,您可以使用每个值循环遍历其所有值

#2


2  

You can extend the config file by creating custom configurations. Essentially you will end up with :

您可以通过创建自定义配置来扩展配置文件。基本上你最终会得到:

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

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

Alternatively, you could specify the key as the name of the site and just use http://cephas.net/blog/2003/09/26/extending-webconfig-in-aspnet/ sort of thing.

或者,您可以将密钥指定为站点的名称,只需使用http://cephas.net/blog/2003/09/26/extending-webconfig-in-aspnet/。

#3


2  

Just to give full working sample as this question keeps getting hits

只是为了提供完整的工作样本,因为这个问题不断受到欢迎

NameValueCollection appSettings = ConfigurationManager.AppSettings;

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