Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Read web.config Settings

0.00/5 (No votes)
23 Jul 2015 1  
Read web.config Settings

I don’t know about you, but I always forget how to read settings from web.config file. So, I wrote a static class that will help me with that and I’d like to share it with you.

1. Read Connection Strings

public static string GetConnectionStringUsingConfigurationManager(string nameOfConnectionString)
{
    return System.Configuration.ConfigurationManager.ConnectionStrings[nameOfConnectionString].ToString();
}

public static string GetConnectionString(string nameOfConnectionString)
{
    //null indicates default web.config
    //you can set it to what ever config file in you need
    System.Configuration.Configuration config =
        System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(null);
    if (config.ConnectionStrings.ConnectionStrings.Count > 0)
    {
        var connString = config.ConnectionStrings.ConnectionStrings[nameOfConnectionString].ToString();
        return connString;
    }
    return "";
}

Here are two ways to read connection string from web.config file.

We could also modify GetConnectionString method to take another parameter filePath to use different XML file than default web.config.

2. Read App Settings

public static string GetAppSettingUsingConfigurationManager(string customField)
{
    return System.Configuration.ConfigurationManager.AppSettings[customField];
}

public static string GetAppSetting(string customField)
{
    System.Configuration.Configuration config =
        System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(null);
    if (config.AppSettings.Settings.Count > 0)
    {
        var customSetting = config.AppSettings.Settings[customField].ToString();
        if (!string.IsNullOrEmpty(customSetting))
        {
            return customSetting;
        }
    }
    return null;
}

appSettings is a key-value element in web.config file where we can store our own settings.

Example of appSettings:

<appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    <add key="DatabaseINILocation" value="C:\config\config.ini" />
    <add key="testPort" value="49610" />
</appSettings>

3. Read Section

public static string ReadFieldFromSection(string sectionName, string field, string attribute)
{
    var section = (XmlElement)ConfigurationManager.GetSection(sectionName);
    if (section.GetElementsByTagName(field).Count > 0)
    {
        XmlElement element = (XmlElement) section.GetElementsByTagName(field).Item(0);
        var fieldValue = element.Attributes[attribute].Value;
        return fieldValue;
    }
    return null;
}

This code is very specific to sections that you want to read so you would have to probably modify it little bit.

Example of section that you can read with this code:

  <log4net>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="log/log.txt"/>
      <appendToFile value="true"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %level %logger - %message%newline%exception"/>
      </layout>
    </appender>
  </log4net>

And finally, how can you call these methods from your code:

ReadConfig.GetAppSetting("testPort");
ReadConfig.GetAppSettingUsingConfigurationManager("testPort");
ReadConfig.GetAppSettingUsingConfigurationManager("testPort");
ReadConfig.ReadFieldFromSection("log4net", "file", "value");
ReadConfig.GetConnectionString("TestDatabase");
ReadConfig.GetAppSettingUsingConfigurationManager("TestDatabase");

You can download the complete class here: ReadConfig.cs.

Hope it will help you.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here