In yesterday's we looked at different web.config setting to help optimize a Sitefinity website in terms of caching and compression of static content. Naturally you don't want caching to get in the way while developing and constantly having to resort to app-recycles and clearing of the browser cache.

Enter web.config transformations

A perhaps not-very-well-known feature yet it has been available for a few years.

Simply open your project in Visual Studio 2010 or 2012, right-click your web.config and click "Add Config Transforms". Nested under your web.config, you'll find 3 new files called web.Debug.config, web.Release Pro.config and web.Release.config.

Each of those files correspond with one of the Visual Studio solutions configurations.

VisualStudio Solution Explorer screenshot showing web.config transformations
Fig: VisualStudio Solution Explorer with web.config transformations

This will work with any ASP.NET application out-of-the-box, but instead of the usual 2 (debug & release), Sitefinity comes with 3 configurations and we'll take advantage of that extra one by using it for our caching.

Anything you'd like to be always present you setup in the web.config. Anything you'd like to see changed, you simply put in the corresponding config file. On each build, Visual Studio will insert/remove/merge the transformation rules setup in the corresponding for you.

  • Would you like Elmah but just when you're debugging?
    Simply add 5 lines of code to the web.Debug.config.
  • Do you want different url-rewrite rules for your testing & debug environment?
    Simply add the changes to each of the .config files.
  • Only enable caching & compression when you're in production mode?
    That's right - add the snippets from yesterdays post to your web.Release Pro.config.
Well, it's almost that easy... Microsoft wouldn't be Microsoft if they hadn't made things a bit more difficult, and thus they introduced the xdt prefix.

XDT stands for XML-Document-Transform

in laymen terms, is nothing else than a namespace that defines two attributes: Locator and Transform. The first defines where and the second describes what to do like for instance InsertBefore, Replace and Remove.

If you open the web.Release.config file in VisualStudio, you'll see the most useful one already inserted for you

All this does is remove the compilation tag debug of the web.config. Simple, elegant and no more excuses to accidentally run production sites in debug-mode. To apply this to our caching & compression snippets from yesterday, we simply need to enhance them with a bit of markup.

To achieve this:

Our web.Release Pro.config will contain this:

The <system.webServer> surrounding our <caching> helps the transformations locate our tag and ensures it goes into the correct section. By adding the xdt:Transform="Insert" to the <caching> we tell VisualStudio to insert it into our newly built web.config And that's all there's to it.

More information

See a fully working Sitefinity 5 compatible example web.Release.Config on GitHub.
To read more take a look at the MSDN article on the xdt syntax
Or watch the Mix2010 'Web Deployment Made Awesome' demo by Scott Hanselman .