Go to manage web. config from XML file by using configsource attribute ASP. NET 2.0

Source: Internet
Author: 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 specified ettings, connectionstrings and all settings are needed to manage in separate XML files, 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 external file for that section. using an external configuration source can be useful in your scenarios. for instance, you cocould 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 eachWeb. configSection like appsettings. XML, connectionstring. XML, and set the file inApp_dataFolder to protect.

Copy the each section fromWeb. configAnd 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 > 

InWeb. configChangeAppsettingsTags

<Appsetemediconfigsource ="App_data \ webconfigxml \ appsettings. xml"/>

The configsource attribute must be a relative physical path.

Repeat this for eachWeb. configSection (connectionstrings, SMTP ).

Now you can manage the XML file from secure page by usingGridviewControl 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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.