Custom settings in Web.Config

by Sander Gerz June 03, 2005 20:22

Ever felt the need to switch settings in your web application based on the server it was running on? Well, I have. Suppose you have a connection string in your Web.Config (encrypted, sure). You don't want to be changing the configuration manually everytime you switch from development, to testing and to production. So here's what I do.

First, you create a new section in the Web.Config file.

    <mySettings>
        <!-- development -->
        <Configuration url="http://localhost"> 
            <add key="ConnectionString" value="server=(local);database=Northwind;user id=sa;password=..." />                     
            <add key="SettingWhatEver" value="true" /> 
        </Configuration> 
        <!-- testing -->
        <Configuration url="http://testserver"> 
            <add key="ConnectionString" value="server=testserver;database=Northwind;user id=dbusr_test;password=..." />                     
            <add key="SettingWhatEver" value="true" /> 
        </Configuration> 
        <!-- production -->
        <Configuration url="http://liveserver"> 
            <add key="ConnectionString" value="server=prodserver;database=Northwind;user id=dbusr_prod;password=..." />                     
            <add key="SettingWhatEver" value="false" /> 
        </Configuration>         
    </mySettings>

The mySettings element is inserted as a child of the configuration root element in your Web.Config.

Then, you create a custom section handler for the Web.Config file.

Imports System
Imports System.Xml
Imports System.Configuration
Public Class MySettings
    Inherits Hashtable
    Implements IConfigurationSectionHandler
    Public Function Create(ByVal parent As Object, ByVal configContext As Object, ByVal section As System.Xml.XmlNode) As Object _
        Implements System.Configuration.IConfigurationSectionHandler.Create
        Dim HostUrl As String = HttpContext.Current.Request.Url.Host
        Dim NodeSetting As XmlNode
        Dim NodeSettings As XmlNodeList
        Dim Settings As New Hashtable
        NodeSettings = section.SelectNodes("Configuration[contains(@url,'" & HostUrl & "')]/add")
        For Each NodeSetting In NodeSettings
            Me.Add(NodeSetting.Attributes.GetNamedItem("key").Value, NodeSetting.Attributes.GetNamedItem("value").Value)
        Next
        Return Me
    End Function
End Class

This section handler will read from the mySettings element, and select the node with the url-attribute that contains the HostUrl value. This is a simple XPath expression, perhaps you want to finetune it depending on your needs.

Finally, you go back to the Web.Config and make sure the section is understood and read by adding this element to the Web.Config

<configSections> 
  <section name="mySettings" type="WebApp.MySettings, WebApp" /> 
</configSections>  

The first attribute holds the custom section name in the Web.Config. The second attribute holds the class that will handle this section (WebApp.MySettings) and the assembly where this class can be found. The section handler is located right under the configuration root element, but before the mySettings element mentioned above. Be aware that XML is case-sensitive.

Now that your done, you can access the desired setting through:

Dim constr As String = Context.GetConfig("mySettings")("ConnectionString")

Depending on the host you are running the web application on, this will return the development, testing or production connection string.

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

.NET

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen | Modified by Mooglegiant