Add Reference External Config Files in Web.Config

Introduction

In this blog we will discuss how can add external config files in web.config in ASP.NET.

web.config

It contains all application level, project level, IIS level settings like cookie, session state, database connection string, url rewriting etc in XML format(so that it will compatible in every machine).

Sometimes we need to refer another config file in web.config.

Suppose our web.config file is already contains so many keys, settings. And we want to add 200-300 keys in web.config file, so in that case web.config is difficult to maintain. So we need add one more config file which will contain 200-300 keys.

Let's look below how it is implemented:

Code for web.config 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <configuration>  
  3.    <appSettings file="web1.config">  
  4.    </appSettings>  
  5. </configuration>  
In web.config file we refered another config file by file="web1.config".

web1.config - In this file you can direct declare the tags except xml tag.

Because this file content will append to Web.config. See below:

Code for web1.config:
  1. <appSettings>  
  2.    <add key="name" value="manas mohapatra"/>  
  3. </appSettings>  
Now AppSetting key(name) will be available in web.config file.

Conclusion

Adding external config files makes code more maintainable, elegant.

So use external config files whenever your web.config file contains huge amount of keys and setting.
 
Happy Coding!!