How to add a list or several values ​​for one key in web.config in asp.net

How to add a list or several values ​​for one key in web.config?

For example: I have a key called "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 available.

+7
source share
4 answers

Add to web configuration as comma separated

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

access them as

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

You cannot do this directly, but with a bit more encoding, you may have a custom configuration section in your configuration files. The https://msdn.microsoft.com/en-us/library/2tw134k3.aspx link describes how to perform custom configuration.

Another way could be to use a separator in a value and define multiple values, and then use those values ​​using the split function (as mentioned above)

+2
source

Define the delimiter character.

Then set the application parameter by its key and use string.Split to get these several values ​​as an array or IEnumerable<string> :

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

You can use the appsettings tag

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

C # code

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

Source: https://habr.com/ru/post/1232090/


All Articles