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

"Zero Web.config Configuration" Deployment / "No touch web.config" Application Deployment

0.00/5 (No votes)
2 Dec 2004 1  
No web.config modifications required, for deployment.

Introduction

"Zero web.config Configuration" or "No Touch web.config" Deployment of Web Application.

All of us are aware, that when we are moving the application from one environment to another (i.e. Development to Test, Test to Validation and so on), we have to change web.config file to use proper settings pertaining to that server. A good example would be the connection string. It is different for each sever (Development, Testing, Validation, Production).

There is a neat way of doing this, which if implemented, you will not have to change anything in the web.config, when moving from one server to another, it will automatically pick up the appropriate key values depending on the server your application is running from. (Please note that purpose of this article is to introduce you to a concept and is not adhering to the code naming convention etc. You can improve on the code and variable names declaration etc.)

Using the code

Warning: Remember XML files are case sensitive and if you make mistake in creating a proper tag, your ASP.NET applications will not run, so you need to be very careful. I would suggest to take a backup of your Machine.config files before modifying it and replace it with the backup file in case it creates a problem.

There are three steps to achieve "No web.config modification" deployment of web application.

Step 1

You will create a key in the Machine.config file of the server/Machine, to identify that server as specific environment (i.e. Development, Testing, Validation, Production). Search the Machine.config file for "appSettings". This key is already present but commented, modify it as shown in the figure and identify your Server (here I have identified it as development server, name it according to the kind of server it is or naming conventions your company uses).

    <appsettings>
        <add key="Environment" value="Development" />
    <appsettings>

Step 2

Next create the following key in your web.config of your application as shown here. (Note you can also move the following settings to Machine.config, but for the sake of simplicity, I have included them in web.config, once you are comfortable with the concept, you may move this as well to the Machine.config, so there is very less to be done on the part of the developer and in case there is change to any of the setting, you have only one place to change). You can always customize name and values according to your need.

<configSections>
  <section name="Development"
        type="System.Configuration.SingleTagSectionHandler,system,
        Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  <section name="Testing"
        type="System.Configuration.SingleTagSectionHandler,system, 
        Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
   <section name="Production"
        type="System.Configuration.SingleTagSectionHandler,system, 
        Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
     
     <Development
            ConnectionString="DevelopmentConnectionString"
            ErrorString="DevelopmentErrorConnectionString"
            setting3="third value" />
    
    <Testing
            ConnectionString="TestConnectionString"
            ErrorString="TestErrorConnectionString"
            setting3="third value" />
            
    <Production
            ConnectionString="ProductionConnectionString"
            ErrorString="ProductionErrorConnectionString"
            setting3="third value" />

Note: Incase you want to use different settings, irrespective of what is set in the Machine.config file, you can create the same key with different values in the web.config file.

<appsettings>
    <add key="Environment" value="Development"/>
<appsettings>

This will work because, when you access the key, ASP.NET will first search the Web.config for that key. If it finds it, it will return the value, if it does not find it, it will search Machine.config file and return the value if the key is found, else return nothing.

Step 3

Now we want to read the values in our application, for this I have created a small function to which you can pass the name of key and it will return the value for that key, the function also accepts optional parameter name �Environment�, which if passed, will return the key pertaining specified environment.

Public Function GetKeyValue(ByVal _Key As String, _
                        Optional ByVal _Environment As String = "")
      Dim Env As String

      If Trim(_Environment) <> "" Then
            Env = _Environment
      Else
            Env = ConfigurationSettings.AppSettings("Environment")
      End If
            Dim config1 As System.Collections.IDictionary = _
                                   ConfigurationSettings.GetConfig(Env)
            Return config1(_Key)
End Function

Next Comes How To Read the Value

To retrieve the value of the key, you can call the function like this:

         Response.Write(GetKeyValue("ConnectionString"))

The above statement will return the appropriate connection string depending on which environment the application is running on. If it is running on Development, it will return development Connection string, if it is running on Test Server, it will return Connection string related to test server.

Also in case it is needed, you can specify, that you need the connection string pertaining to specific environment or server, you can do that as mentioned below.

        Response.Write(GetKeyValue("ErrorString", "Testing"))

Life made easier !

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