VS2012 Web Deploy, Using Transforms For Connection String In Production (Web.Config)

Posted by Peter Kellner on March 31, 2013 · 4 mins read

 

Introduction

I should have learned this a long time ago.  I certainly have known about it since VS2010 when it was introduced, I’ve read about it multiple times, I just thought I did not have the time to learn it so for the past couple years, I’ve been making one web.config change after another and of course making all the stupid little mistakes we all make (I think) when we do things manually.

So, in 25 minutes this morning, I updated my http://siliconvalley-codecamp.com site with Web.Config transforms so now, when I “publish” with Visual Studio 2012 I just pick the correct profile (either beta4 or svccprod) and when I do the publish command, I get the correct database connection strings.  svcodecamptest for beta4 and svcodecamp for production.  Here is what I’ mean, then I will go into more details to show just how simple it really is.

Deploy To Production Deploy To Test
image image
image

 

Nuts and Bolts

To make the above a reality, it is really quite simple.  Here are the steps (and for a more complete description, read here:  http://msdn.microsoft.com/en-us/library/dd465318(v=vs.100).aspx (that is where I learned it in under 30 minutes).

First, create two new configuration profiles.  In my case I just used the nice wizard that says copy an existing profile.  I copied the release profile.  The screen shot is something like this:

image

In the “Active solution configuration” dropdown, the last choice is “create new” and from there you can create a new configuration copying an existing one.  My current one shown is “beta4”.

Then, go to your vs2012 solution explorer, right click on your real web.config and choose “Add Config Transform”.  It will be highlighted if you have a new one like you added above (I’ve already added my transforms).

image

Now, this next step is the really cool easy part if I had known about I would have done a long time ago.  All you have to do is copy the section you want in this particular configuration (and will be web deployment) your named config and add 2 attributes to that xml (for that named config).  Wow!, that’s it.  I feel so stupid for thinking this was going to be some XSLT transform learning party before I could use this.

image

Finally, you go to your publish wizard from your “Project” and say “Publish”, make sure you have a configuration set for each server you want to publish to as follows (I’m showing my production one)

image

And that is it!

 

Conclusions

I’m annoyed I did not learn this 2 years ago.