Split the Web.Config
When we work on web projects, we usually encounter with web.config files, not one but may be many at the same time. These web.config files are ASP.NET configuration files. They can be distributed throughout the application directories to enable independent configuration at directory levels without affecting the configuration settings at higher directory.
Which is the top most configuration file?
This file is called Machine.config and it is located at
The Machine.config file is used for server-wide configuration settings. All web application inherits the configuration settings from this file and if the application has its own web.config, then the configuration settings available in application’s web.config override some of the machine.config settings.
Note:There are some of the machine.config settings which cannot be overridden in configuration files that are lower down in the hierarchy.
Web.config is available at the root of the application and it inherits all of the settings in the Machine.config file. So, we just need to provide the new settings in web.config which overrides the default settings. When the application loads the configuration settings, it starts from the most local Web.config file (which is located in the current directory, if it exists, or a parent directory).The local configuration settings override settings in parent configuration files.
We have a simple chart here,
Note: These settings are calculated once and then cached across subsequent requests.The application is restarted when a configuration change is made.
Example: Split the Web.config
Now, Just split the web.config settings for example,
create an external file “ConnectionString.config” to store all the connection strings in one file and provide this path in web.config.
“ConnectionString.config” stores only the connectionStrings section. There are no extra elements, sections, or attributes.
<connectionStrings> <add name="ConnectionName" providerName="System.Data.ProviderName" connectionString="SampleConnection String;" /> </connectionStrings>
At the root of the application, web.config uses the configSource attribute to specify the location of the external file.
<!--?<span class="hiddenSpellError" pre=""-->xml version='1.0' encoding='utf-8'?> <configuration> <connectionStrings configSource="ConnectionString.config"/> </configuration>
Configuration settings is the foundation for any web application or website. There is a lot of flexibility provided by .Net framework to define and manage the configuration settings.