Introduction
The configSource
attribute was firstly introduced in .NET Framework 2.0 to support external configuration files.
After uploading my website to an online server, I needed to manage the website setting from Web.Config, therefore I saved appSettings
, connectionStrings
,
and all settings that I needed to manage in separate XML files, to ease managing and organizing.
This can be done by using the configSource
attribute. The configSource
attribute was firstly introduced in .NET Framework 2.0 to support external configuration files.
This attribute can be added to any configuration section to specify an external file for that section. Using an external configuration source can be useful
in many scenarios. For instance, you could place a section into an external configSource
if you need an easy method to swap settings for the section depending on the environment.
Using the code
First create an XML file for each web.Config section like appSettings.xml, ConnectionString.xml …, and set the file in the App_Data folder to protect.
Copy each section from web.config and set in XML file as in the following example (appSettings.xml):
="1.0"="yes"
<appSettings>
<add key="EnableErrorPage" value="false" />
<add key="RequiredLogin" value="false" />
<add key="PublicationsEmail" value="email@domain.com" />
<add key="AdminFromEmail" value="email@domain.com " />
<add key="AdminToEmail" value="email@domain.com " />
<add key="SupportEmail" value="email@domain.com" />
</appSettings>
In Web.config, change the appSettings
tag to:
<appSettings configSource="App_Data\WebConfigXML\appSettings.xml"/>
The configSource
attribute must be a relative physical path.
Repeat this for each web.config section (connectionStrings
, smtp
, …).
Now you can manage the XML file from a secure page by using a Gridview
control to edit and update the values.
Conclusion
The web.config file is smaller and hence easier to read, and you don't need a complete web.config file for each environment.
Greetings, I'm Mohammad sbeeh.
I have a lot of experience in the Microsoft product stack. During the past years I have been fully dedicated to working on Microsoft .NET, SharePoint, Office 365, Azure and Business Intelligence solutions for the enterprise.
I have been involved in a lot of different projects, including SharePoint, .NET, Office 365 and Azure projects. Mainly as lead developer, solution architect and the bridge between business and technology.
I am Microsoft Certified Trainer for the past 6 years. I delivered more than 100 training on .NET, SharePoint, Office 365, Azure, SQL Server, and Business Intelligence inside and outside Jordan.
I founded SBEEH Software, a consultancy office in Jordan focusing on .NET, SharePoint, Office 365, Azure and Business Intelligence BI technologies
http://www.sbeeh.com