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

來源:互聯網
上載者:User

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

 

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.

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.