Customize the configuration section for personalized web. config

Source: Internet
Author: User
You can easily access the data of the ettings node in the web. config Configuration file through the ConfigurationSettings attribute of the System. Configuration class. It is very convenient and useful for web programs to use this configuration file to store read-only program information, such as program name, author information, and database connection string. For example:
<! -- Sample. aspx -->
Private void Page_Load (object sender, System. EventArgs e)
{
This. tbName = ConfigurationSettings. Receivettings ["AppName"];
}
<! -- Web. config -->
<Configuration>
<Deleetask>
<Add key = "AppName" value = "MyApplication"/>
</AppSettings>
......
For the ConfigurationSettings class, you can use the GetConfig (string sectionName) method to access any configuration element. For the preceding example, you can use the following method:
<! -- Sample. aspx -->
Private void Page_Load (object sender, System. EventArgs e)
{
Object settings = ConfigurationSettings. GetConfig ("deleetask");
NameValueCollection nvc = settings as NameValueCollection;
If (nvc! = Null)
{
String val = (string) nvc ["AppName"];
This. tbName = val;
}
}
It can be seen that the GetConfig () method returns a configuration processing object. After being converted to a NameValueCollection instance, you can access the content in this section. In fact, the configuration file retrieval is implemented by the processing program. At the same time, we can see the declaration of the processing program in web. config or machine. config, such:
<! -- Web. config -->
<Configuration>
<ConfigSections>
<Section name = "mySection"
Type = "Chagel. Configration. Settings, Configuration"/>
</ConfigSections>
<MySection>
<AppName> MyApplication </AppName>
</MySection>
......
The above declares a mySection element and declares in configSections that the Configuration's handler class is named Chagel. Configration. Settings, and Configuration is the assembly name. Next, we can use a class that implements the System. Configuration. IConfigurationSectionHandler interface to process this Configuration element, for example:
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.