How can we add list or multiple values for a single key in web.config in asp .net

与世无争的帅哥 提交于 2019-12-23 09:12:49

问题


How can we add list or multiple values for a single key in web.config?

For example: I have key named "xyz" it has a list of values, that is , val1, val2, val3 etc.

And this can be obtained in my code as other keys are accessible.


回答1:


Add in web config as comma separated valued like

<add key = "xyz" value="val1, val2, val3"/>

access them as

  string[]  xyzValues = System.Configuration.ConfigurationManager.AppSettings["xyz"].Split(",");



回答2:


You can't do it directly but with a little more coding you may have custom config section in your config files. https://msdn.microsoft.com/en-us/library/2tw134k3.aspx link describes how to do custom config.

Other way could be use a separator in value and define multiple values and then use that values using split function(like other have mentioned)




回答3:


Define a separator character.

Then, get the application setting by its key and use string.Split to get those multiple values as an array or IEnumerable<string>:

IEnumerable<string> values = ConfigurationManager.AppSettings["xyz"].Split(',');



回答4:


You can use the appsettings tag

<appSettings>
  <add key="xyz" value="val1;val2;val3" />
</appSettings>

C# Code

string[] values = ConfigurationManager.AppSettings["xyz"].Split(';');


来源:https://stackoverflow.com/questions/32737620/how-can-we-add-list-or-multiple-values-for-a-single-key-in-web-config-in-asp-ne

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!