Click here to Skip to main content
15,881,559 members
Articles / Web Development / ASP.NET

Manage Web.config from an XML file by using the configSource attribute in ASP.NET 2.0

Rate me:
Please Sign up or sign in to vote.
3.95/5 (10 votes)
9 Oct 2007CPOL1 min read 79.2K   25   12
How to manage Web.config from an XML file by using the configSource attribute in ASP.NET 2.0.

Screenshot - coolcode.gif

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):

XML
<?xml version="1.0" standalone="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:

XML
<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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect SBEEH Software
Jordan Jordan
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

Comments and Discussions

 
GeneralMy vote of 4 Pin
jay@begginer3-Sep-13 21:45
jay@begginer3-Sep-13 21:45 
GeneralCustom configuration sections Pin
Sipke10-Feb-09 2:03
Sipke10-Feb-09 2:03 
How can I use configSource for my own custom configurationSections?
Right now, when I set configSource="custom.config", ASP.NET throws a configuration exception, stating that the attribute configSource cannot be used because it uses the reserved word config or lock.
Any ideas?
GeneralRe: Custom configuration sections Pin
Om Rudi13-Feb-09 18:52
Om Rudi13-Feb-09 18:52 
AnswerRe: Custom configuration sections Pin
Mohammad Sbeeh13-Feb-09 20:18
Mohammad Sbeeh13-Feb-09 20:18 
GeneralRe: Custom configuration sections Pin
Om Rudi14-Feb-09 8:57
Om Rudi14-Feb-09 8:57 
QuestionApplication restart ? Pin
epaquet16-Oct-07 16:26
epaquet16-Oct-07 16:26 
AnswerRe: Application restart ? Pin
Mohammad Sbeeh17-Oct-07 5:12
Mohammad Sbeeh17-Oct-07 5:12 
AnswerRe: Application restart ? Pin
Roger Martin18-Oct-07 8:22
professionalRoger Martin18-Oct-07 8:22 
GeneralOne suggestion Pin
tmorrison11-Oct-07 16:41
tmorrison11-Oct-07 16:41 
GeneralRe: One suggestion Pin
Mohammad Sbeeh11-Oct-07 19:47
Mohammad Sbeeh11-Oct-07 19:47 
GeneralRe: One suggestion Pin
dollychopra18-Jul-10 20:09
dollychopra18-Jul-10 20:09 
Generalthanks.. Pin
Michael Sync9-Oct-07 23:03
Michael Sync9-Oct-07 23:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.