Wednesday, August 22, 2018

Web.config Transform (Visual Studio)



Introduction:

Sometimes, you may encounter a situation that you want your web.config of staging server to be different with the web.config of production server. For example, the connection string of staging server should be different from that of production server (one is for test database, and one is for client database).  

How to create our own transform web.config?

Note: Originally, VS create “Debug” and “Release” configuration for us.

If we want to create our own configuration, select “VS -> Build -> Configuration Manage”.

Then you can see the following picture to create a new configuration.

After creating our customized configuration, on Solution Explorer right click on the web.config, then click “Add Config Transform”.


Then, you can see the one we created has been shown.

How to know the difference through using different transform config?


As default, VS will create “Debug” and “Release” configuration for us. We can use those to be example to declare this question. We found there is only one-line difference between Debug and Release.

<compilation xdt:Transform="RemoveAttributes(debug)" />

From above instruction, we assume that on “Release” build we want to delete the debug attributes.

On Solution Employer, we can right click our project and select Publish…

Then select Folder option.

Then go the file path “source/repos/ConfigTransform/ConfigTransform/bin/Release/PublishOutput” to check the transform web.config.

"Release" version of web.config as shown as following picture.


On the other hand, we want to change the build configuration to “Debug”.
Go to Publish page, click Settings…


And change Configuration to Debug.

Then you can notice that the web.config has been changed! The Debug version contains debug information.


Conclusion:

According above, we can use web.config transform to define the different setting between staging server and production server.

No comments:

Post a Comment