Everything you wanted to know about Web.Config
Web.config files have become more and more complicated and their more subtle intracies are often left a mystery to many.
This article will aim to collate many interesting things about the web.config. I am starting this qucikly and will just add to it when I can.
configSource
Using configSource to create a seperate file for configuration.
Typically you may have some appSettings in your web.config looking something like this:
<appSettings>
<addkey="SiteName"value="MySite"/>
<addkey="Username"value="MyUsername"/>
<addkey="Password"value="MyPassword"/>
</appSettings>
Another option at your disposal though is to have your appSettings in a seperate file and point the appSettings node in the direction of that file.
So lets say we created a folder in the root of our web app and called it App_Config and in there we placed a file called AppSettings.config. We could point our appSettings node in our web.config file to our seperate AppSettings file in one of the following two ways.
Now as far as I'm aware you only have the option of using "file" for the appSettings but you can use configSource for any of the sections.
Now there are two very interesting and useful things about these two methods. Firstly if you use file then you can have name key values in your web.config file and in your seperate file and duplicate names in the seperate file will override those in the web.config. If however you use configSource then you cannot place any name value pairs in the main web.config - they all have to in the seperate file.
Another interesting and useful feature is to do with application restarts. Normally changes to a web.config file cause an application to restart. This is by design. The web.config is normally read when an app starts and then cached. Any changes to the web.config will not take effect until the app restarts - hence the reason for web.config changes causing an app restart. So don't make any web.config changes unless you want your app to restart!
It would I think be nice though if we could reconfigure a web app and have it change behaviour on the fly without restarting. Restarting is obvioudly bad as you can lose sessions etc. In fact many developers put configuration values not in the web.config but in a database maybe to avoid this restarting problem.
Now the really useful point to this is that if you use configSource then any changes you make to your seperate file will not cause a restart but will be read by the configuration readers in real time without any nasty restarting!
Using file on the other hand doesn't cause a reatart either but also doesn't have any config values reflected in code until a restart is made some other way.