简体   繁体   中英

How to Read Items into Arrays in web.config

I'm trying to build an array of strings from items in a web.config file (in IIS).

web.config

<appSettings>
    <add key="favourite" value="url1; site1" />
    <add key="favourite" value="url2; site2" />
    <add key="favourite" value="url3; site3" />
</appSettings>

C#

// Reads first favourite into string.
public string _favourites = ConfigurationManager.AppSettings ["favourite"];

I would like each favourite to be read into string [ ] _favourites array with the semi-colon (I would parse that out later). web.config is an XML file so I can open it as one and pull the data out, but is there an easier way to do this using ConfigurationManager?

What if you add all Array values in single key like -

<appSettings>
    <add key="favourite" value="url1;site1,url2;site2,url3;site3" />
</appSettings>

Read that key value as a string -

public string _favourites = ConfigurationManager.AppSettings["favourite"];

and then split the string by ','(comma) like this -

string[ ] _favouritesArr = _favourites.Split(',');

This will give all values in array _favouritesArr.

I don't know if there is a hack to do it, but I would just have one setting with multiple values;

<appSettings>
    <add key="favourite" value="url1;site1;url2;site2;url3;site3;" />
</appSettings>

or

<appSettings>
    <add key="favourite" value="url1=site1;url2=site2;url3=site3;" />
</appSettings>

Another solution is a separate configuration file or store this in a database.

I imagine that the favourites would change and changing web.config has consequences - it may cause your app to restart.

How about: 1.right click on project > properties 2.Navigate to settings. If it shows, This project does not have settings file. click to create one. 3.Create a SystemCollections.Speciliazed.StringCollection 4.Name it, as you want (my example StatusReason). and in end of line on e right there is ...(three) dots 5.Press them and add as much settings needed. One per line. Sample:

Valid=979580000
Invalid=979580001
Broken=979580002
ReadyForCollect=979580003
Missing=979580004
Refine=979580005

and in web config it looks like that.

<applicationSettings>
<WebApplication.BookServices>
     <setting name="StatusReason" serializeAs="Xml">
            <value>
              <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                              <string>Valid=979580000</string>
                <string>Invalid=979580001</string>
                <string>Broken=979580002</string>
                <string>ReadyForCollect=979580003</string>
                <string>Missing=979580004</string>
                <string>Refine=979580005</string>
              </ArrayOfString>
            </value>
          </setting>
</WebApplication.BookServices>
</applicationSettings>

and get your array of strings :

var settings = Settings.Default.StatusReason;
//TODO add any logic, if needed to split

add your project reference to properties:

using WebApplication.BookServices.Properties;

and there you have a array of settings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM