Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 746
Japneet Singh Avatar asked Sep 23 '15 10:09

Japneet Singh


People also ask

Can we have multiple config file for single project?

Yes you can have two web. config files in application. There are situations where your application is divided in to modules and for every module you need separate configuration. For example if you have a application which has two modules lets say accounts and sales.

Can we have 2 web config in asp net?

You can have multiple configuration files in your asp.net project. There is no restriction to use the web. config file in the asp.net web application.


2 Answers

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(",");
like image 87
Imad Avatar answered Oct 11 '22 06:10

Imad


You can use the appsettings tag

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

C# Code

string[] values = ConfigurationManager.AppSettings["xyz"].Split(';');
like image 2
Ricky Alexandersson Avatar answered Oct 11 '22 06:10

Ricky Alexandersson