Tuesday, December 11, 2012

Automatically create different Web.Config files for Debug and Release builds in Visual Studio


This is a follow up on a post I did a couple of months back called, use both local and production connection string in the Web.Config using visual studio 2010, and I will here give you some more “meet” so you can use this in a more practical way.
When working on projects you will most likely use the Web.Config to store different parameters like connection strings to databases and other application settings. This settings are often specific to the machine you are developing on and will not likely be the same on the production server or the staging server. In Visual Studio there is built in support to have different values in the Web.Config file depending on what configuration you are using when you are building your projects. By default Visual Studio creates two configurations, Debug and Release.

Default usages of configurations in Visual Studio 2010

By default Visual Studio will use different configurations for different operations, it will use the Debug configuration when debugging your application on your local machine and it will use the Release configuration when you build your application using the Publish function.
image

 

Setting a different connection string for the Release configuration

A very useful thing to do automatically is to have the connection strings in your application automatically change to point to your production servers whenever you publish your web application. To do so you simply add the connection string element in the Web.Release.config file and add the following two xdt attributes
xdt:Transform="Replace" xdt:Locator="Match(name)"
What this does is it will replace the connection string matched by the name attribute.
Your connection string in Web.Release.config would now look like the following.
xdt:Transform="Replace" xdt:Locator="Match(name)" />
This code will replace the connection string in Web.config with the attribute Name=”DB” with the new connection string pointing to the production server.

Remove the debug=”true” attribute from the Release configuration

Another thing that is good to do when publishing your web app to the production servers is to disable debug mode by removing debug=”true” in the compilation section in web.system in the web.config file. This is easily done with a another xdt:Transform attribute called RemoveAttributes(name)
Just add the following code in Web.Release.config.
This will remove the attribute debug=”true” when publishing using the Release configuration.

Setting different custom error pages for the Release configuration

Lets say you are developing your app on a local machine and want to disable custom errors in the application during development you can do this by replacing the customErrors element in Web.config.
The following will let you enable custom errors to point to /errors/index.htm when your app is running in production code.

More transformation operators

There is three ways of locating the element you want transformed.
  • Condition(XPath expression)
  • Match(Comma-delimited list of one or more attribute names)
  • XPath(XPath expression)
There is also
  • Transform=”Replace”
  • Transform=”Insert”
  • Transform=”InsertBefore(XPath expression)”
  • Transform=”InsertAfter(XPath expression)”
  • Transform=”Remove”
  • Transform=”RemoveAll”
  • Transform=”RemoveAttributes(Comma-delimited list of one or more attribute names)”
  • Transform=”SetAttributes(Comma-delimited list of one or more attribute names)”
These are mostly self explaining in what they perform so I want go into details on this, you can read more about them onMSDN.

No comments: