Continuous Integration, Web

ASP.NET Core web.config publishing blues

1 min read

I’ve been working on being able to use Octopus Deploy to deploy ASP.NET Core applications through IIS and I was faced with an interesting challenge. When publishing to a File System through the Visual Studio Publish command, it will trigger a PowerShell script that essentially will call Microsoft Web Deploy V3 (msdeploy) to put your FileSystem up to date with your release. But before calling MSDeploy, it will update (or should I say overwrite) your web.config with the proper handlers that match your application. This means that it will create a generic web.config and removing all the settings you may want. The ASP.NET team is aware I believe of the fact that the publishing tool is limited and thus in need of some customization features wise.

Using Octopus Deploy as a deployment solution, there are some solutions which you can take to fix the above situation.

One solution is to have a copy of your web.config say, web.{environment}.config and replace the original web.config with that said web.{environment}.config file. I find that approach not as smooth as I would like. MSDeploy uses variables to replace certain attributes values and this means you will have to maintain that on each of your environment web.config.

I chose another approach where I run a PreDeploy PowerShell script that will add the proper configurations to my web.config before the application is deployed.
Here is the PreDeploy PowerShell script that I use to set the Rewrite Rule for HTTP to HTTPs and to set some custom headers. Feel free to use any other language that Octopus Deploy supports (C#, F#, etc).

The script can definitely be improved, but it is simple enough that I did not bother 🙂

There are probably other ways that I am not aware of and I would love to hear from you so don’t be shy to comment or to tweet me!