Intellisense For AppSettings/ConnectionStrings

One of the most time consuming things to do when starting a new web project is setting up the sites directory structure, until recently. I finally took the time to create myself a few generic templates for Visual Studio enviroment.  This has made my ramp up time on starting a new project close to nil. I have my folder stucture all set.  I created a few Classes that I typically create in all my projects, add a small library of default images and now just recently dialed the application in with jQuery wired up. Check out Sara Ford's article on making project templates in VS 2005 (Works the same in VS 2008). http://blogs.msdn.com/saraford/archive/2008/10/16/did-you-know-you-can-create-project-templates-336.aspx

 

One of the files in my project template is a class file I call SiteConfigs. This file contains all my values in my webconfig files appsettings and connectionstrings. Putting them in this file saves me the time of typing in ConfigurationManager.AppSettings("WebDomain").ToString() everytime I want to access my webconfig. It also provides me with intellisense for all my keys in my appsettings/connectionstrings. Now all I do is...

 

Response.Write("Code Version: " & SiteConfigs.CodeVersion)

 

Here's an example.

 

<WEB.CONFIG> 

 

<appSettings>

    <add key="WebUrl" value="127.0.0.1"/>

    <add key="WebDomain" value="127.0.0.1"/>

    <add key="ApplicationName" value="/"/>

    <add key="CodeVersion" value="1.0.0"/>

</appSettings>

<connectionStrings>

    <add name="SiteCS" connectionString="Data Source=;Initial Catalog=;Persist Security Info=True;User ID=;Password=" providerName="System.Data.SqlClient"/>

    <add name="ADConnectionString" connectionString=""/>

</connectionStrings>

 

<SITECONFIG.VB> 

 

Public Class SiteConfigs

 

#Region "APPSETTINGS"

 

    Public Shared ReadOnly Property WebUrl() As String

        Get

            Return ConfigurationManager.AppSettings("WebUrl").ToString()

        End Get

    End Property

 

    Public Shared ReadOnly Property WebDomain() As String

        Get

            Return ConfigurationManager.AppSettings("WebDomain").ToString()

        End Get

    End Property

 

    Public Shared ReadOnly Property ApplicationName() As String

        Get

            Return ConfigurationManager.AppSettings("ApplicationName").ToString()

        End Get

    End Property

 

    Public Shared ReadOnly Property CodeVersion() As String

        Get

            Return ConfigurationManager.AppSettings("CodeVersion").ToString()

        End Get

    End Property

#End Region

 

#Region "CONNECTIONSTRINGS"

 

    Public Shared ReadOnly Property SiteCS() As String

        Get

            Return ConfigurationManager.ConnectionStrings("SiteCS").ToString()

        End Get

    End Property

 

    Public Shared ReadOnly Property ADConnectionString() As String

        Get

            Return ConfigurationManager.ConnectionStrings("ADConnectionString").ToString()

        End Get

    End Property

 

#End Region

 

End Class

2 Comments

Comments have been disabled for this content.