Thursday, April 24, 2014

Reading ASP.NET web.config file (ConnectionString and AppSettings) from classic ASP (.asp)

Classic Microsoft ASP is still out there and being used. In most of the classic projects I've been involved in the classic website is in a state of migration to a new ASP.NET website. In most cases in a hybrid form. Moving forward on the project requires unifying the configuration so that the two applications can co-exist and run as one application. 

To make that easier I wrote a simple classic asp class that could read connection string and app settings from a .NET web.config file.

The class is designed to be able to support multiple different web.config files (and in the case connection strings) support a config source. This can allow you to externalize your connection strings from your web.config files.


Usage:


The class is initialized the same way as any other object in classic asp:
Dim config 
Set config = new Configuration
It should be disposed of like any classic asp object:
Set config = Nothing
By default the class is initialized with a default configuration file named “web.config”. You can modify it by setting the configuration file property:
Dim config 
Set config = new Configuration

config.ConfigurationFile = "web.config"
Reading a app setting:
Dim config 
Set config = new Configuration

config.AppSetting("appLevel")

Set config = Nothing
Reading a connection string:
Dim config 
Set config = new Configuration

config.ConnectionString("YourConnectionStringName")

Set config = Nothing
Note: This works for a referenced config source or embedded in configuration file


Code:

Class Configuration
    'class sub
    Private Sub Class_Initialize 
        m_ConfigurationFile = "web.config" 'set default configuration file (for web applications)
    End Sub

    'configuration file
    Private m_ConfigurationFile
    Public Property Get ConfigurationFile()
        ConfigurationFile = m_ConfigurationFile
    End Property
    Public Property Let ConfigurationFile(p_ConfigurationFile)
         m_ConfigurationFile = p_ConfigurationFile
    End Property

    Public Property Get ConfigurationFileFullPath()
        ConfigurationFileFullPath = Server.MapPath(m_ConfigurationFile)
    End Property

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' Business/Feature Functions
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Public Function AppSetting(p_AppSettingName)
        Dim objXMLDoc
        Set objXMLDoc = LoadConfigurationXmlFile() 'load the configuration file

        Dim n_AppSettings
        Set n_AppSettings = objXMLDoc.selectSingleNode("configuration/appSettings/add[@key='" + p_AppSettingName + "']")

        If IsNull(n_AppSettings) Or (varType(n_AppSettings) = vbEmpty) Or (IsObject(n_AppSettings) = False) Or (n_AppSettings is Nothing) Then
            Call Err.Raise(15002, "AppSetting", "AppSetting " + p_AppSettingName + " does not exist")
        Else
            AppSetting = n_AppSettings.GetAttribute("value")
        End If

        Set n_AppSettings = Nothing
        DestroyXmlObject(objXMLDoc)
    End Function

    'ConnectionString
    'Read a connection string from a web.config (or a referenced .config file)
    Public Function ConnectionString(p_ConnectionStringName)
        Dim objXMLDoc
        Set objXMLDoc = LoadConfigurationXmlFile() 'load the configuration file
        
        Dim n_ConnectionString
        Dim s_attributeConfigSource
                
        Set n_ConnectionString = objXMLDoc.selectSingleNode("configuration/connectionStrings")
        s_attributeConfigSource = n_ConnectionString.GetAttribute("configSource")
            
        If IsNull(s_attributeConfigSource) Or s_attributeConfigSource = "" Then
            ConnectionString = ReadFromConnectionStringNode(n_ConnectionString, p_ConnectionStringName) 
        Else
            'response.Write(s_attributeConfigSource)
            Dim objXMLConnectionStringDoc

            Set objXMLConnectionStringDoc = CreateXmlObject()
            objXMLConnectionStringDoc.load Server.MapPath(s_attributeConfigSource) 'load the configuration file

            Set n_ConnectionString = objXMLConnectionStringDoc.selectSingleNode("connectionStrings") 'set the connection string node to read from this file.

            ConnectionString = ReadFromConnectionStringNode(n_ConnectionString, p_ConnectionStringName) 

            DestroyXmlObject(objXMLConnectionStringDoc)
        End If

        Set s_attributeConfigSource = Nothing
        Set n_ConnectionString = Nothing

        DestroyXmlObject(objXMLDoc)
    End Function

    'ReadFromConnectionStringNode
    'Query out connection string information from the connection string node. It will contain
    'zero to many connection strings so we will find the correct one by looking for the connection
    'string name using an XPath statement.
    'Errors:
    '15001 - connection string not found
    Private Function ReadFromConnectionStringNode(n_ConnectionString, p_ConnectionStringName)
        'query for the connection string information
        Dim n_configInfo
        Set n_configInfo = n_ConnectionString.selectSingleNode("add[@name='" + p_ConnectionStringName + "']")               

        If IsNull(n_configInfo) Or (varType(n_configInfo) = vbEmpty) Or (IsObject(n_configInfo) = False) Or (n_configInfo is Nothing) Then
            Call Err.Raise(15001, "ConnectionString", "Connection String " + p_ConnectionStringName + " does not exist")
        End If

        ReadFromConnectionStringNode = n_configInfo.GetAttribute("connectionString") 'return the connection string information from the xml file

        Set n_configInfo = Nothing
    End Function

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' Utility Functions
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Private Function LoadConfigurationXmlFile()
        Dim objXMLDoc
        Set objXMLDoc = CreateXmlObject()   
        objXMLDoc.load Me.ConfigurationFileFullPath() 'load the configuration file

        Set LoadConfigurationXmlFile = objXMLDoc
    End Function

    Private Function CreateXmlObject()
        Dim objXMLDoc
        Set objXMLDoc = Server.CreateObject("MSXML2.DOMDocument.3.0")    
        objXMLDoc.async = False    

        Set CreateXmlObject = objXMLDoc
    End Function

    Private Function DestroyXmlObject(p_XmlObject)
        Set p_XmlObject = Nothing
    End Function
End Class
Key Words:
Classic asp, web.config, classic asp web.config, classic asp read connection string from web.config, classic asp read appsettings from web.config

References:
http://en.wikipedia.org/wiki/Active_Server_Pages
http://en.wikipedia.org/wiki/Asp.net

1 comment:

Unknown said...

Thank you Christopher!

It works like a charm!