[英]How to get values from web.config app key to a list
I have the following in my web.config: 我的web.config中包含以下内容:
<add key="someValuestoReturn" value="60,59,58,57,56"/>
I want to return a list of this integer values into my my method but i'm having a difficult to call the list of the values from the web.config: 我想将这个整数值的列表返回到我的方法中,但是我很难从web.config调用值列表:
Private int GetValues(){
var _list = System.Configuration.ConfigurationManager.AppSettings["someValuestoReturn"].ToList(); //<< doesn't work
return _list
}
What would be the best way to achieve this? 实现这一目标的最佳方法是什么?
Thank you 谢谢
I might suggest you to solve this on another way.. 我可能建议您以其他方式解决此问题。
lets first create an a empty list of a strings which we are going later to put into an array: 首先创建一个空字符串列表,然后将其放入数组中:
After that we need to find your key and we need to get values from a key into our new created list, but we must take care about spliting because your values are comma separated, so check my code below: 之后,我们需要找到您的密钥,并且需要将密钥中的值获取到新创建的列表中,但是由于您的值是逗号分隔的,因此我们必须注意拆分,因此请查看下面的代码:
List<string> values = new List<string>();
foreach (string key in ConfigurationManager.AppSettings)
{
if (key.StartsWith("someValuestoReturn"))
{
string value = ConfigurationManager.AppSettings[key].Split(',');
values.Add(value);
}
}
string[] myValuesfromWebConfig = values.ToArray();
And that's it, you get all of your values stored in a array called myValuesfromWebConfig
就是这样,您将所有值存储在名为
myValuesfromWebConfig
的数组中
And if you want something else than a string, just change your list/array type from string
to int
for example.. 而且,如果您想要的不是字符串,则只需将列表/数组类型从
string
更改为int
。
As you can see I included 如您所见,我包括了
string value = ConfigurationManager.AppSettings[key].Split(',');
HERE MORE ABOUT Split method 有关拆分方法的更多信息
because your values are comma separated, and I guess you would like to add them to a array/list separately.. :) 因为您的值是用逗号分隔的,所以我想您想将它们分别添加到数组/列表中。
Thanks 谢谢
EDIT: 编辑:
Because of your error that you wrote in comment, we can skip adding it to an a array, simply delete that line, you will have your values stored in list values
anyway, so at the end it migth look like this: 由于您在注释中写的错误,我们可以跳过将其添加到数组中的操作,只需删除该行,无论如何您都会将您的值存储在列表
values
,因此最后它看起来像这样:
List<string> values = new List<string>();
foreach (string key in ConfigurationManager.AppSettings)
{
if (key.StartsWith("someValuestoReturn"))
{
string value = ConfigurationManager.AppSettings[key].Split(',');
values.Add(value);
}
}
Now if you need to read that values or do whatever you want with them, you can loop throught them and that's it, for example: 现在,如果您需要读取该值或对它们执行任何操作,则可以遍历它们,就是这样,例如:
foreach(var item in values)
{
//Do whatever you want with your item
}
A string is returned not a List
. 返回的字符串不是
List
。
For a List
use: 对于
List
使用:
var _list = System.Configuration.ConfigurationManager.AppSettings["someValuestoReturn"].Split(",");
System.Configuration.ConfigurationManager.AppSettings["someValuestoReturn"].Split(',');
它将返回一个字符串数组
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.