Click here to Skip to main content
15,891,184 members
Articles / Web Development

Web.Config Transformations when Debugging

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
16 Sep 2011CPOL2 min read 40K   4   6
Web.Config transformations when debugging

I am a big fan of the Web.Config Transformations that got introduced a while back. Prior to them being inherently supported, one could accomplish the same thing with part of the Enterprise Library, and prior to that, one could leverage a custom MSBuild Task or the XMLPoke Task to get your web.config updated based on the environment it was targeting.

Having your configuration elements managed with a transform file makes it a bit easier to manage and keep track of your settings that could change based on environment or settings. Without this, each developer on your team might need to create host entries and keep their folder structure the same to ensure the settings work for each of their machines. Another option is each team member just has their own configuration files with their specific settings that they need but when source control is introduced, this can get tricky because one developer might be overwriting another's settings.

Unfortunately, VS.Net doesn't do any transforming when you are developing and just debugging your local environment. But there are some steps you can do to make this happen if you want.

  • First, create the configurations you want in VS.Net, assuming the default debug and release are not enough for what you are trying to accomplish.
  • Right click on you web.config and select Add Config Transforms - this will create a dependant transformation config for each of your configurations defined.
  • Now you can rename your web.config to web.base.config.
  • Add a web.config to your project. It doesn't matter what is in it because it will get overwritten every time we do a build but we want it as part of the project so VS.Net doesn't give us the "Your Project isn't configured for Debugging" pop-up.
  • Edit your .csproj Project File and add the following TransformXml task to the AfterBuild target. Here, you can see that I will be transforming the web.base.config file using the web.[configuration].config and it will save it as web.config.

Screen1

For aesthetics, I renamed my transformation files to just web.[configuration].config instead of web.base.[configuration].config and updated the project file like below:

Screen2

So the transformation files are nested under the web.base.config and the web.config doesn't have any transformation files because it is the output not the input. Below, you can see the setup I have so far for the Ergo project where my web.config would look quite different depending on whether I am using Ektron or NHibernate as my data repository.

Screen3

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Independent Consultant
United States United States
___________________________
J A M E S C O L E M A N
Director, Technical Services
Linked-In: http://www.linkedin.com/in/jameswcoleman
Blog: ledtalks.wordpress.com

Comments and Discussions

 
SuggestionAdd code not screenshots Pin
drzaus1324-Apr-13 3:24
drzaus1324-Apr-13 3:24 
GeneralRe: Add code not screenshots Pin
James Coleman24-Apr-13 3:30
James Coleman24-Apr-13 3:30 
Sorry. It is because I actually posted this article through my blog and Code Project really shrunk those images down. My Posterous blog wasn't the best at handling code so I used screenshots. If you visit the original blog post you will be able to see the code much better (click on the screenshot and you get original resolution).

http://ledtalks.posterous.com/webconfig-transformations-when-debugging

~james
___________________________
J A M E S C O L E M A N
Director, Technical Services
iCrossing
http://ledtalks.posterous.com

GeneralRe: Add code not screenshots Pin
drzaus1324-Apr-13 5:46
drzaus1324-Apr-13 5:46 
GeneralMy vote of 5 Pin
Jakub Januszkiewicz20-Sep-11 9:44
Jakub Januszkiewicz20-Sep-11 9:44 
GeneralRe: My vote of 5 Pin
James Coleman22-Sep-11 9:53
James Coleman22-Sep-11 9:53 
GeneralRe: My vote of 5 Pin
Jakub Januszkiewicz22-Sep-11 11:24
Jakub Januszkiewicz22-Sep-11 11:24 

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

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