Reading appSettings from config

I noticed I might have googled for this same tech question more than 5 times, so this time I wrote my own post in case.

Reading appSettings from config should be as simple as:

var numberInString = ConfigurationManager.AppSettings["MaxNumberOfSearchReturnPosts"];
return numberInString == null ? 0 : Int32.Parse(numberInString);

While on MSDN we can find another post for web.config, which supports reading from runtime! Real web hosting environment.

System.Configuration.Configuration rootWebConfig1 =
if (rootWebConfig1.AppSettings.Settings.Count > 0)
var numberInString = rootWebConfig1.AppSettings.Settings["MaxNumberOfSearchReturnPosts"];
return numberInString == null ? 0 : Int32.Parse(numberInString.Value);
return 0;

Note, passing null argument for OpenWebconfiguration() means reading from root path of your host web site. MSDN Document doesn’t state this very clearly.

Looks this this method can also read from a different site! Cool.





Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s