现在的位置: 首页 > 综合 > 正文

转:Manage Web.config from XML File by using configSource attribute ASP.Net 2.0

2012年12月24日 ⁄ 综合 ⁄ 共 1822字 ⁄ 字号 评论关闭

original article: http://www.codeproject.com/KB/aspnet/Manage_Webconfig.aspx 

 

Screenshot - coolcode.gif

Introduction

The configSource attribute was firstly introduced in .NET framework 2.0 to support external configuration files.

After upload website to online server, I need to manage the Website setting from Web.Config, therefore, I save the appSettings, connectionStrings and all settings are needed to manage in Separate XML files, to ease to manage and organize.

This can be done by using 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 a 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 XML file For Each web.Config section like appSettings.xml, ConnectionString.xml �, and set the file in App_Data Folder to protect.

Copy the each section from web.config and set in XML file as 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 the Web.config change the appSettings tags 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 secure page by using 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, and help me to manage the website setting.

 

抱歉!评论已关闭.