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

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.

<add name="DB" connectionString="Server=ProductionServer;Database=DB;User ID=UserID;Password=PWD;" 
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.

<compilation xdt:Transform=”RemoveAttributes(debug)” />

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.

<customErrors redirectMode=”ResponseRewrite” defaultRedirect=”/errors/index.htm” mode=”On” xdt:Transform=”Replace” />

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 on MSDN.

Generating a Default Local Resources automatically in Visual Studio 2010 for a single page

This is just a small tip I want to share for all you who are about to localize your ASP.NET applications.

You can use Visual Studio to automatically generate the default version of your local resource files. Doing so extracts the page and controls into a resource file, it adds the meta:resourcekey attribute to all controls in the page and creates the corresponding key in the local resource file.

  1. Open you webpage in the markup (Design view).
  2. From the Tools menu, select Generate Local Resource. This causes Visual Studio to perform the following tasks
  1. Generate the App_LocalResources folder if necessary.
  2. Create the local resource file with the name <filename>.resx and store it in the App_LocalResources folder. The file will contain resource settings for page elements (such as title) and control properties such as Text, ToolTip, Title, Caption and other string based properties.

If you want to know more about this you can read my other blog post about localization and globalization support in ASP.NET.

I hope this might save some time for you.

Use both local and production connnection string in the web.config using Visual Studio 2010

In this post I will describe how to use both a local connection string and a remote connection string at the same time in a Visual Studio 2010 project.
First of, what you need to do is get some grip of the concept of builds for your project. There are two default build types out of the box, debug and release.

The difference between Debug and Release
The main difference between the both is that Release build strips out all debug information in from your project.
For example, in your web.config for a debug build this section would look like this.

<system.web>

<compilation debug=true targetFramework=4.0>

And for the release build it would be like the following.

<system.web>

<compilation targetFramework=4.0>

Therefore when pressing debug (F5) in Visual Studio you will be running your code using the web.config from debug mode.
That is debug=trueis in set.

This is very important when it comes to the use of two connection strings, one for your local server and one for the production server.
To avoid the time consuming and unsecure process of having to rename a connection string before publish to production (yes we have
all done that before), Visual Studio 2010 comes with a new feature called Add Config Transforms. If you right-click the web.config you see
Add Config Transforms in the context menu.

Selecting this will create two “partial” web.config files. One named Web.Debug.config and one Web.Release.config.

If your project contains others then the default build types they too will get a “partial” Web.<name>.config file.
If we open up the Web.Debug.config file it looks like this.

<?xml version=1.0?>

<configuration xmlns:xdt=http://schemas.microsoft.com/XML-Document-Transform>

<system.web>

</system.web>

</configuration>

What you can do here is set the connection string properties and the custom error properties. This will then use the
web.config as a template and replace the nodes from either the Web.Debug.config or Web.Release.config.

So when we later publish our project we can choose to publish as a release build and we will then have the connection string
properties set in Web.Release.config file in our Web.config file. No more hassle of remember to rename property names. =)

The Windows Azure and Visual Studio

Follow

Get every new post delivered to your Inbox.