Thursday, February 5, 2015

ASP.Net Web.config Transformation

Intro

Did you know there's an easy way to change your web.config when you deploy it? This doesn't involve you manually modifying the web.config after or prior to deployment, or writing some special script to edit the file for you on the live server. Nay fellow coders, nay! All you have to do is create something called a transformation file, put a little bit of xml in it, and when you deploy, the web.config that gets deployed will be magically transformed by Visual Studio. Let's see how the magic is woven.

Note: This blog was done using VS 2013 Community Edition. VS 2013 Express should work the same. Earlier and later versions of VS may differ.

Howdie-Do

First, fire up Visual Studio and create a new Web Application. Make it an empty web application. We don't need any special technologies like Web Forms or MVC as the site won't do anything. We're just demoing how to transform web.config files.

You should now have a web application in Visual Studio that looks like the below screenshot. It's pretty sparse, but that's ok; that's what we want. Notice how your web.config file can be expanded with that little arrow to the left of it? Go ahead and click that. You'll see that 2 files live underneath web.config, namely web.debug.config and web.release.config.


These 2 gems are where we cast our spell. First things first though, let's see what's in the web.config. Open 'er up. Yours should look similar to the one below. Not much in here either, and that's perfect. It will make illustrating this technique easier.


The key element for our purposes will be the compilation tag. See that debug="true" in there? It's generally considered bad practice to leave that in your deployed web.config file, as it creates potential performance issues and security vulnerabilities. Our goal then is to have the configuration transformation process set debug to false or remove it.

OK let's get crackin! Open up your Web.Release.config. I'll assume that when you want to push your website live you'll be building the solution in release mode, but I'll show you how to set the compilation mode during deployment in a little bit.

This file has more content than the web.config. Still not very many tags though. And hey, what's that? A compilation tag? Yeppers it is! Visual Studio has assumed for you that you'll want to modify the compilation tag when deploying in release mode. Most importantly, see the xdt:Transform attribute? This attribute, with its value of "RemoveAttributes(debug)", tells Visual studio to remove the debug attribute from your web.config during deployment. Well slap me and call me Jill, that's precisely what we wanted. Yeehaw! No coding necessary even!


Now it's time to see how to deploy the site. Right-click the web application project in Solution Explorer and choose the menu item "Publish".


Normally you might deploy to Azure or something else equally nifty, but for the sake of brevity we'll deploy to the local machine. Choose "Custom" as the publish target. A popup window will come up asking what you want to name the publish profile. Name it whatever you like.

 

On the next screen choose "File System" as the publish method, and point the Target location wherever you like on your computer.

  


On the next screen of the wizard choose "Release" as your Configuration, then click the "Publish" button.


Once VS is done publishing, use Windows Explorer to browse to the directory you published the site to. Open up the deployed web.config in whatever text editor you like. See that lovely compilation tag? No debug attribute! Success! FYI, if the debug attribute is not present, its value defaults to false.


What's Next?

There is a lot more you can do with Web.config Transformation files. You can replace elements, remove elements, add elements, add/remove/modify attributes and more. Check out the link below in the Resources section to see how to do all that you could possibly want and then some.

Resources

Web.config Transformation Syntax

No comments:

Post a Comment