简体   繁体   中英

Query web.config for a key - in values

Can any one let me know how to get a bool result, verifying a value exists in a web.config's key.

Scenario is,

I have this tag in my website...

<add key="isEnabled" value="False"/> for a website,

On this key value I keep my site 'on' and 'off' using

public static bool isEnabled = Convert.ToBoolean(WebConfigurationManager.AppSettings["isEnabled "]);

if(isEnabled)
{
//
}

now the requirement is got 3-4 websites now, want to change the above line to something like

<add key="SitesEnabled" value="1,4,5"/>

because i want to enable only 1st, 4th, 5th site

1 - is the static value for my 1st website, 2 - 2nd.....

But now how do I do on and off...my websites something like

public static bool OneSiteEnabled = Convert.ToBoolean(WebConfigurationManager.AppSettings[SitesEnabled="1"]); // true

public static bool TwoSiteEnabled = Convert.ToBoolean(WebConfigurationManager.AppSettings[SitesEnabled="2"]); //false

Please let me know ...Thanks

I would do it something like this:

using System.Linq;

var sitesEnabled = 
    ConfigurationManager.AppSettings["SitesEnabled"] != null 
        ? ConfigurationManager.AppSettings["SitesEnabled"].Split(',') 
        : new string[0];

var oneSiteEnabled = sitesEnabled.Contains("1");
var twoSiteEnabled = sitesEnabled.Contains("2");

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