Wednesday, January 11, 2012

Use web.config transfromation in visual studio

Generally most of programmers maintain their connection string and other setting of live environment in signal web.config with comment to switch in to live environment and will do manual changes whenever need to change environment.
However Visual studio gives you facility to make this transformation automatically, so today I would like to explore this idea here.


When you create new project in Visual studio 2010, it has separate configuration for debug and release as you can see in following fig.




Read more about this 2 environment at msdn.


You can set specific configuration from toolbar and create new environment as well and add new configuration source under web.config as per shown in below figs.







Let's take an example in web.Release.config write connection string as per shown in below fig.




Transformation actions are specified by using XML attributes which is mapped to the xdt prefix.
In the above example "SetAttributes" transform will change the value of "connectionString" to use "ReleaseSQLServer" only when the "Match" locator finds an attribute "name" that has a value of "MyDB".


Read more about syntax on msdn

Look at video about this here

No comments: