Click here to Skip to main content
Click here to Skip to main content

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

, 9 Oct 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
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 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:

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

Share

About the Author

Mohammad Sbeeh
Team Leader Consulting Company with specialist skills in Porta
Jordan Jordan
Mohammad Sbeeh born in November 24th, 1983 in Amman - Jordan, He is Senior Web Developer(Information technology Department) in Information Technology International company.

The higher education in Philadelphia University, IT - Software Engineering Department, I have been programming and web developer for 4 years, and have been working with the .NET framework 1.0 and 2.0 since the beginning of 2003, in both VB.NET and C#.
 
http://www.sbeeh.com

Comments and Discussions

 
GeneralMy vote of 4 Pinmemberjay@begginer3-Sep-13 22:45 
GeneralCustom configuration sections Pinmembersico10-Feb-09 3:03 
GeneralRe: Custom configuration sections PinmemberOm Rudi13-Feb-09 19:52 
AnswerRe: Custom configuration sections PinmemberMohammad Sbeeh13-Feb-09 21:18 
GeneralRe: Custom configuration sections PinmemberOm Rudi14-Feb-09 9:57 
QuestionApplication restart ? Pinmemberepaquet16-Oct-07 17:26 
AnswerRe: Application restart ? PinmemberMohammad Sbeeh17-Oct-07 6:12 
AnswerRe: Application restart ? PinmemberRoger Martin18-Oct-07 9:22 
GeneralOne suggestion Pinmembertmorrison11-Oct-07 17:41 
GeneralRe: One suggestion PinmemberMohammad Sbeeh11-Oct-07 20:47 
GeneralRe: One suggestion Pinmemberdollychopra18-Jul-10 21:09 
Generalthanks.. PinmemberMichael Sync10-Oct-07 0:03 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141216.1 | Last Updated 10 Oct 2007
Article Copyright 2007 by Mohammad Sbeeh
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid