Referencing external config files from Web.Config

Referencing external config files from Web.Config

Anybody programming with ASP.NET knows that you can store custom application’s settings in the web.config file, under the section, and programmatically read the values through the ConfigurationSettings class. When the application starts, the web.config is read and all its settings are cached, so that there isn’t a IO operation every time a value is needed. The web.config file is also automatically monitored for changes, and when a change is detected the file is read and cached again, but more importantly, the application is restarted and all the pages in the same directory and the subdirectories are recompiled on the next request, because the new settings of the web.config file might affect the page’s behavior (think about smart navigation, security, cookies and sessions, for example).

This means that if you frequently change the custom keys in the section, you’ll cause frequent recompilation of the pages, and thus slow down the application. You can easily avoid this problem, at least for custom application settings, by storing the settings in an external file referenced from web.config. This option, undocumented but present also in ASP.NET 1.0, allows you to change your custom settings without editing the web.config file, and thus avoid the application restart. The drawback is that if you actually want to reload the settings, you have to manually force an application restart, for example by making a fake change to web.config.

Let’s see how to put this tip in practice. Create a file named ExternalWeb.Config (you can use any name you want, but we suggest using the .config extension, so that the user can’t download and view it) with just the tag and its keys, as you would do in web.config:


Then, in web.config write the tag with the file attribute, that specifies the virtual path of the external configuration file written above. The settings stored in ExternalWeb.Config will be added to those defined in the web.config’s :


As a last note, if your external file has a key with the same name of a key defined in web.config, the key in the external value will override the value stored in web.config.

Note: This tip applies to Windows Forms’ configuration files too! (In VS.NET this file is named app.config, but after compilation it is copied to the bin folder as your_app_name_here.exe.config)

See also  How to Create and Deploy QR Codes Online: A Comprehensive Guide

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist