Tuesday, February 24, 2015

How To: Split Web.Config Sections To Multiple Files

Why Split Web.config Sections to multiple files :

1- Easy to maintain.
2- Organizing the Work.
3- Avoid The Error " Cannot read configuration file because it exceeds the maximum file size "

Let's begin:

- We will Split the following sections to separated files:
1- AppSettings.
2- ConnectionStrings.
3- SafeControls.
4- Bindings.
5- Client.

In every section you would like to split you should follow the following steps :

1- In Web.Config Section Cute the entire section contents.
2- Create New File and name it as you want but end with .config E.g. "appsettings.config"
3- Paste the section contents inside the file you just created plus the section tag it self


=======================================================
Web.Config :

  <appSettings file="appsettings.config">
  </appSettings>
---------------
appsettings.config : 

<?xml version="1.0" encoding="UTF-8"?>
<appSettings>
     <add key="MyKey" value="MyValue" />
     <add key="MyKey2" value="MyValue2" />
</appSettings>
---------------
=======================================================
Web.Config :

  <connectionStrings configSource="conn.config">
  </connectionStrings>
---------------
conn.config :

<connectionStrings>
       <add name="CNName" connectionString="Data Source=MyServer;Initial Catalog=MyDB;User ID=MyUser;Password=MyPassword;Pooling=false" providerName="System.Data.SqlClient" />
</connectionStrings>
=======================================================
Web.Config :

<SafeControls configSource="SafeControls.config">
</SafeControls>
---------------
SafeControls.config :

<SafeControls>
  <SafeControl Src="~/_controltemplates/*" IncludeSubFolders="True" Safe="True" AllowRemoteDesigner="True" SafeAgainstScript="True" />
  <SafeControl Assembly="XXX.XX.XX, Version=1.0.0.0, Culture=neutral, PublicKeyToken=XXXX" Namespace="XXX.XX.XX.XX" TypeName="*" Safe="True" SafeAgainstScript="False" />
</SafeControls>
=======================================================
Web.Config :

<bindings configSource="bindings.config" />
---------------
bindings.config: 

<bindings >
</bindings>
=======================================================
Web.Config :

<client configSource="client.config" />
---------------
client.config : 

<client>
</client>



@naadydev
naadydev@outlook.com

1 comment: