How to setup Web config transformation before build on Visual Studio

In common Visual Studio, ASP.NET MVC projects there’s a great necessity to be able to debug different enviroments, which have different Web.Configs, i’ve tried the SlowCheetah and it didn’t work in my case, i’m not sure if thats because i publish to Azure or not, anyway, follow this tutorial.

For starters, copy your Web.config file within the same folder and call the file Web.Base.Config. This will be your base configuration on which the final Web.Config will look like. You should get something like this in Visual Studio.

Web.config example

You see there’s three environments there that we want to transform our main Web.config with:

  • Debug
  • Staging
  • Production

The final step consists on adding a piece of code into your project’s csproj file. Its located at the Projects root folder like this: <projectname>.csproj

Scroll down to the very final and place the following code just before the closing </project> tag.

<Target Name="BeforeBuild" Condition="'$(PublishProfileName)' == '' And '$(WebPublishProfileFile)' == ''">
    <TransformXml Source="Web.Base.config" Transform="Web.$(Configuration).config" Destination="Web.config" />

This fix is really useful if you have many different environments and you need the ability to test them in your development environment. Let me know in the comments if this helped you out or if you have any questions đŸ™‚