Managing configuration with multiple environments in Visual Studio - the right way!
It's very common we develop a web application using App.config/Web.config. The config usually has different values for different environments - connection Strings, URL's etc... When publishing the web application to the server, it uploads the config file together. If you don't know how to handle the configuration in Visual Studio properly, you will find yourself overwriting the config file in every environment for every publish.
So let me show you the right way to do it:
Let's say we developed Web API Service in the development environment, And we have in the Web.config a connection String named 'MyDB', which points to the development environment:
In order to publish this site with a different value of the connection String, we will have to do the following:
1. Go to the Configuration Manager:
2. Choose '<New..>' in the 'Active solution configuration':
3. Add the name of the environment in your organization, for example, 'QA'. You can copy the existing config file to save time, instead of writing the config again
4. After you press 'OK', validate that your application has its correct config file name, and close the editor:
5. Go back to your Web.config, and notice when you open it that you will see 'Web.Debug.config' and 'Web.Release.config'. The 'Web.Debug.config' is for the development environment, and the 'Web.Release.config' is for the production environment. (I recommend that you read more about the release, because it is doing a lot of optimizations on the .dll files, which sometimes causes the site to act differently from the regular .dll's.)
6. Now, to see the your new 'QA' config file, right click the 'Web.config', and choose 'Add Config Transform':
7. Look inside the QA file: basically, if you don't write anything; it will take the same config as the 'Web.config'. If you have this declaration in your file:
'<compilation xdt:Transform="RemoveAttributes(debug)" />
You will not be able to debug this environment, so if you if you want to be able to perform debug - so, remove this declaration.
Let's say you want to change the value of your 'MyDB' connection Strings. Then copy the connection Strings, change the value, and add this declaration:
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
at the end of your connectionString. This will tell the config to replace the connectionStrings with the same name as the current value. There are more options, such as 'Delete' etc... You are invited to read more about them here:
8. Create Publish Profiles: right click on your web application and click 'Publish':
9. For this demonstration, we will publish our site to a folder in the server. So, choose 'Folder' as the publish target, insert the location, and in the bottom choose 'Create Profile' and click
10, Now click 'Actions' -> 'Rename Profile', give it a normal name, and save:
11. Now, let's connect the 'Publish Profile' and the right config file. click 'Configure' -> 'Settings' -> choose the right config file:
If you will have problems with the publish of the configuration, try selecting 'Delete all existing files prior to publish', under 'File Publish Options'.
12. Now return 1-11 steps to add more configs and profiles as the number of the environments you have.
Last thing, you should pay attention that now you have for every config separate folder in the bin, so if you have problems with debugging etc... you should ensure that your running on the right environment:
Thanks,
Ziv Ben-Or
+972545882011
Senior Software Engineer at Evolve Group
1 年Which extension or Nuget package did you use, Slow Cheetah, Fast Koala or other?
Software Consultant
3 年As a .NET novice, I found this article Very informative!