How to Change Web Configuration

I am trying to explain how to automate the process of changing a Web Configuaration in ASP.NET.

The web transformation would be helpful because:

1. It is a bit difficult to make the same set of changes for a different environment.

2. If the config file has more entries then it would be difficult to traverse manually.

3. Possibilities of typo error.

ASP.NET 4.0 has provided a very nice option to transform the web configuration.

When we create an ASP.NET application using Visual Studio, it will create a web config file with the following two transformations.

  1. Web.Debug.config: Debug mode
  2. Web.Release.config: Release mode.


When we run the application (publish) in a debug mode, the web.debug.config file would override the web config settings and the Web.Release.config file settings would be used when the application is running in a release mode.

Now I want to do the web.Config transformation for other build environments, like:

  1. Staging.
  2. Production.

To do this, I am using the following procedure.

Open Visual Studio and create a new ASP.NET Web application. When you create a web application, you can see one web config with two transformation files as shown below.

  1. Web.Debug.Config
  2. Web.Release.Config

The default structure of the XML configuration transformation file will be as in the following.



4. Setting up the XML config Transformation file for Staging And Production Environment.

Go to the configuration manager in the Visual Studio as shown below.

.

5. This will allow us to create a new config transformation as well as new.



6. Add a new config transformation file for Staging and Production environment.



7. Now when you create the transform in the web application project all the declared config transform files will be created under the actual Web.Config file node in the Solution Explorer. They will be named as Web.<name provided by you>.config. In order to create them right-click on the Web.Config file and select the "Add Config Transforms" option as shown below.



9. Now, the XML transformation file for the Staging Environment has been added.



10. Have a connection string in the Web.Config file that will be used by the application that we created for data access.

I have added a connection string SQlConncetionstring in the web config file as shown below.



11. Add a connection string (transforms) in the web.staging.config.



A few keywords are being used in the web transformation as given below.

In the transform files there are two attributes that does the tricks.

1. Locator

The locator attribute specifies the config element or elements that need to be transformed. Some of the Locator attribute values are:

  • Condition
  • Match

2. Transform

The transform attribute specifies what kind of transformation that must be performed on the located element. Some of the Transform attribute values are:

  • Replace
  • Insert
  • InsertBefore
  • InsertAfter
  • Remove
  • RemoveAll
  • RemoveAttributes
  • SetAttributes

12. Once the transformation is ready, select the configuration as Staging and publish the web application.

Then you can see the updated connection string over there.