Using Web.Configuration Transformation for Database ConnectionString in ASP.NET


Introduction

Usually, in the web application development process (on a development server), the developer writes his local database connection string in Web.config file and at the time of deployment (on production server) he manually edits the Web.config file to map the databases. But in Visual Studio 2010 we have a brand new feature that is known as Web.config transformation. In this article we are going look at a simple demonstration on configuration transformations. Here you go…

Demonstration

Create a new project by clicking File > New > Project > ASP.NET Web Application on your desktop.

When you done you can see, by default there are two files with Web.config:

image001.jpg

Web.Debug.config

This transformation environment is used by development server (called as debug).

Web.Release.config

This transformation environment will be used by production server (called as release).

How to add new Transformation configuration file

To add new transformation configuration file in project, follow the steps given below.

Step 1

In Visual Studio 2010, click on Build > Configuration Manager… it will pop a window.

Step 2

In the new window, click on drop down list box and click on 'New' there, it will also pop a new window asking for a name, type the name and click on ok button, find the screen below.

image002.jpg

When you are done, right click on the Web.config file and click on the 'Add Config Transformations' option.

image003.jpg

Now you can see we have three transformation configuration file in list.

image004.jpg

In case you can't see your new transformation file or get an error, then fix it by clicking on 'Show All Files' option and there may be some temp file having same name.

image005.jpg

To delete 'Web.Testing.config' file, you need to follow the step directed in step2 and select 'Edit' option instead of 'New' and also delete that file from 'Solution Explorer'.

Web.config File

This is our target file that will be modified when we deploy our web application. Here is my local (development server) configuration stetting, which is normal and most web developer is aware:

<connectionStrings>

  <add name="Database1ConnectionString1" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"

    providerName="System.Data.SqlClient" />

</connectionStrings>

In the above piece of code, we need to change the connectionString values so that we host our database or say map or database on hosting server (production server) too.

Web.Release.config File

The following connectionString will map my database on the production server:

connectionString="Data Source=ip_address_or_localhost;Initial Catalog=yourdatabasefilename;Persist Security Info=True;User ID=yourdbusername;Password=yourdbuserpassword"

Now, look at my 'Web.Release.config' file (code given below), which has magical codes to perform such action. In the code given below, to update connectionStrings we need to place the logical code inside <conectionStrings> tag. Now, we are adding some properties in <add /> tag that will perform our target action. xdt:Locator="Match(name)" will match the node by name, xdt:Transform="Replace" will modify the data by replacing it. 'name' attribute contains the target name value that is "Database1ConnectionString1" in my case and connectionString contain the value which will be replaced. Here is the code:

<connectionStrings>

  <add xdt:Locator="Match(name)" xdt:Transform="Replace" name="Database1ConnectionString1" connectionString="Data Source=ip_address_or_localhost;Initial Catalog=yourdatabasefilename;Persist Security Info=True;User ID=yourdbusername;Password=yourdbuserpassword"/>

</connectionStrings>

To understand the above code snippets, we should first take a look at some abbreviations.

Transform

This attribute informs the transformation engine the way to modify web.config file for specific configuration. Transforms can do following things for us:

* Replacing a node

* Inserting a node

* Delete a node

* Removing Attributes

* Setting Attributes

Locator

This attribute helps the Transformation engine to exactly pin-point the web.config node that the transform from web.Release.config should be applied to. Locators can do are:

* Match on value of a node's attribute

* Exact XPath of where to find a node

* A condition match to find a node

When you done with above all, publish the application locally and test its Web.config file's connectionString value, you can see it is changed.

I have opened my published Web.config file in notepad here

image006.jpg

I hope you like this post. Please post your comments and feedbacks. Love You. Thanks.

Up Next
    Ebook Download
    View all
    Learn
    View all