Click here to Skip to main content
Click here to Skip to main content
Go to top

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

, 2 Dec 2004
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

nssidhu
Web Developer
United States United States
Naunihal Singh Sidhu
Bachelor of Engineering (Electronics & Communication)
HDSE(Higher Diploma in Software Engineering)
MCSD(VB6 track),MCAD(.NET), MCSD(.NET)
 
Senior Software Consultant, having 9 years of experience in developing High End, complex Multi teir Client Server Projects & , web Application , VB5,VB6,VB.NET,ASP,ASP.NET & SQL,Web Service, Remoting etc...

Comments and Discussions

 
GeneralGood article, but... PinmemberRohit Wason28-Feb-07 3:56 
GeneralExcellent Pinmembermisha200014-Dec-04 0:36 
GeneralCai I user more than one web.config files in .Net web application Pinsusssumanth112-Dec-04 18:17 
GeneralRe: Cai I user more than one web.config files in .Net web application Pinmembernsingh197716-Dec-04 7:58 
GeneralRe: Cai I user more than one web.config files in .Net web application Pinmembersabbaghov00706-May-06 22:13 
GeneralGood example of ConfigSettings PinmemberMartyOne9-Dec-04 7:08 
GeneralGood one Pinsusssiddu sing barnala2-Dec-04 11:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140922.1 | Last Updated 2 Dec 2004
Article Copyright 2004 by nssidhu
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid