Split the Web.Config

Split
Split@Image courtesy of Boians Cho Joo Young/ FreeDigitalPhotos.net

Split the Web.Config

Welcome to CodeSpread!

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

systemroot\Microsoft .NET\Framework\versionNumber\CONFIG\Machine.config

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.

Inheritance 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,

image

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'?&gt;

&lt;configuration&gt; &lt;connectionStrings configSource="ConnectionString.config"/&gt;

&lt;/configuration&gt;

Conclusion

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.

Author: hershey

A passion for knowledge drives me to do programming, A passion for programming drives me to create something different, A passion for creation drives me to spread the knowledge.

Share This Post On

0 Comments

  1. nice superb explaination

    Post a Reply
  2. thanks for the tutorial
    I am having a problem.
    The feature “Create SQL server database” isn’t available , I can’t select it.
    Can you help, please ?

    Post a Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

More from CodeSpread:

  • Useful WCF ToolsUseful WCF ToolsWhen we work with WCF, we mostly concentrate on the development aspect which involves create, deploy, and manage WCF applications. As a developer, I can suggest that If we utilize various tools ava...
  • Custom error mode in ASP.NET web.configCustom error mode in ASP.NET web.config Dual Roles As a developer, I would love to see the error page that provides details of the error, status code and also the stack information. This information is useful to debug the application ...
  • Agile Encounters UnpredictabilityAgile Encounters UnpredictabilityWhat is Agile? Wikipedia says “Agile software development is a group of software development methods based on iterative and incremental development, where requirements and solutions evolve through...
  • Don’t Mess with App_OfflineDon’t Mess with App_OfflineOne of the most useful features of ASP.NET is the "App_Offline.htm" . It provides a much easy way to make the ASP.NET application offline while we do our modifications on the site. How we do it? ...
  • How to add alias and create virtual directory?How to add alias and create virtual directory?There is a term very famous in IIS world 'virtual directory', but before discussing a virtual directory, we should be familiar with the term 'alias'.By definition, an object can have alternative na...
  • How to create a MVC 2 Project in VS2010?How to create a MVC 2 Project in VS2010?How to create a MVC 2 Project in VS2010? 1. Open VS2010. 2. Select File -> New Project. 3. Select Visual C# -> Web -> ASP.NET MVC 2 Empty Web Application. Name it as MyMvcApplicat...
  • C# Code to send a MailC# Code to send a MailWhile working in a project, we always neglect the importance of the code which we have written many times and now end up in copy pasting the code from old projects. Lately, I saw a request in fo...
  • Why Multiple Inheritance is discouraged?Why Multiple Inheritance is discouraged?Hi Guys, This is Sathish Again,Today we will discuss why most of the higher languages eliminates Multiple Inheritance? Most of you guys already know that multiple inheritance is not available for...