问题
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