Skip to main content

Dotnet Core: Tranform web.config

Introduction:
In dotnet core 2.2  or above, transformations of the web.config can be done with the following ways:
1. Build Configuration
2. Profile
3. Environment
4. Custom
Any of the processes can be utilized in your project deployment pipeline.

"In Dotnet core  2.2  and above transformation works directly with the core platform. In Dotnet core 2.1 or below transformation won't work directly for that please go through this link here."

Build Configuration:
To create a Dotnet core MVC project, click here for  Dotnet core Mvc get started. Now by default web.config file will not be created. Now add the web.config file to the project and change the file as below.



<configuration>
  <system .webserver="">
    <handlers>
      <remove name="aspNetCore">
      <add modules="AspNetCoreModule" name="aspNetCore" path="*" resourcetype="Unspecified" verb="*">
    </add></remove></handlers>
    <aspnetcore arguments="%LAUNCHER_ARGS%" processpath="%LAUNCHER_PATH%" stdoutlogenabled="false" stdoutlogfile=".\logs\stdout">
      <environmentvariables>
        <environmentvariable name="App_Environment" value="Development">
      </environmentvariable></environmentvariables>
    </aspnetcore>
  </system>

Now add a source file from where transformation needs to be done. Name this file as web.{Configuration}.config,  here Configuration means either Debug or Release. For now, we create this file with the Release configuration as the web.Release.config and change the file as below



<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform" >
  <system .webserver="">
    <handlers>
      <remove name="aspNetCore">
      <add modules="AspNetCoreModule" name="aspNetCore" path="*" resourcetype="Unspecified" verb="*">
    </add></remove></handlers>
    <aspnetcore arguments="%LAUNCHER_ARGS%" processpath="%LAUNCHER_PATH%" stdoutlogenabled="false" stdoutlogfile=".\logs\stdout">
      <environmentvariables>
        <environmentvariable name="App_Environment" value="Production" xdt:Transform="Replace">
      </environmentvariable></environmentvariables>
    </aspnetcore>
  </system>

1. xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform" this schema needs to be added to the root tag on the web.Release.config file
2. xdt:Transform="Replace" , this tag in web.Release.config needs to be replaced with the tag on the web.config
Now to test this transformation open the command prompt with your project as the root path,
run this command
"dotnet publish --configuration Release". After successful execution of command go to path "\bin\Release\netcoreapp2.2\publish" where your project gets published. If you observe the web.config file in the publish folder, we can notice that the environment variable gets replaced with the value from the web.Release.config file.

Profile:
For this approach create the file as web.{profile}.config, here "profile" will be your published profile file name. Lets, for now, create as web.myProfile.config file and copy the content of the web.Release.config(for testing) which we created as above into this web.myProfile.config file.

"dotnet publish --configuration Release /p:PublishProfile=myProfile". After successful execution of command, we can see web.config get replaced with the value from web.myProfile.config file.

Environment:
For this approach create the file as web.{environment}.config, here "environment" can be like "development", "stage", "production" are any custom variables you will use in a project. Now copy the content from the web.Release.config into our file(let us create a file as web.production.config)

"dotnet publish --configuration Release /p:EnvironmentName=production".

Custom:
For this approach create a file as {custom_name}.tranform (here file extension must be .tranform).
To test it copy the content from the web.Release.config  and execute the below command. (let us create this file as custom.transform)

"dotnet publish --configuration Release /p:CustomTranformFileName=custom.transform"

Execution Priority:
we have discussed all type of transformation that can be used dotnet core. The order of this method gets to execute : "Build Configuration" => "Profile" => "Environment" => "Custom".
so based on your requirement just configure any one of the approaches in your deployment pipeline.

Comments