Customizing configuration section Processing to implement personalized Web.config

Source: Internet
Author: User
Tags foreach config implement interface
The web passes the appsettings property of the ConfigurationSettings class in system.configuration to facilitate access to the appsettings node data in the Web.config configuration file. For web programs to use this configuration file to store some read-only program information, such as program name, author information, database connection string, etc. will be very convenient and useful. Such as:

<!--sample.aspx-->
private void Page_Load (object sender, System.EventArgs e)
{
This.tbname = configurationsettings.appsettings["AppName"];
}

<!--web.config-->
<configuration>
<appSettings>
<add key= "AppName" value= "MyApplication"/>
</appSettings>
... ...

For the ConfigurationSettings class there is a method GetConfig (string sectionname) that can access any configuration element, and for the above example, this can be used:

<!--sample.aspx-->
private void Page_Load (object sender, System.EventArgs e)
{
Object settings = ConfigurationSettings.GetConfig ("appSettings");
NameValueCollection NVC = settings as NameValueCollection;
if (NVC!= null)
{
String val = (string) nvc["AppName"];
This.tbname = val;
}
}

The Visible GetConfig () method returns a configuration-processed object that can be accessed after it has been converted to an instance of NameValueCollection. In fact, there is a handler implementation behind the configuration file retrieval, and we can see the declaration of the handler in Web.config, or Machine.config, for example:

<!--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 handler class for the configuration is named Chagel.configration.settings,configuration for the assembly name. We can then handle the configuration element through a class that implements the System.Configuration.IConfigurationSectionHandler interface, such as:

<!--settings.cs-->
Using Chagel.Configration.Data;
Namespace Chagel.configration
{
public class Settings:iconfigurationsectionhandler
{//Implement the Create method for this interface
public Object Create (object parent, object input, XmlNode node)
{
Data data = new data ();

foreach (XmlNode xn in node. ChildNodes)
{
Switch (xn. Name)
{
Case ("AppName"):
Data. AppName = xn. InnerText;
Break
Case ("AppVer"):
Data. AppVer = xn. InnerText;
Break
... ...
}//switch End

}//foreach End

return data;

}//method End
}
}

The IConfigurationSectionHandler interface has only one method, and whenever a discovery is registered to the configuration section of a handler, the Create method is invoked on the section handler, and the class we implement returns an instance of the data class, which is a specialized dataset with the following code:

<!--data.cs-->
Namespace Chagel.Configration.Data
{
public class Data
{
Public Data ()
{
}
public string appname;//program name
public string appver;//Program version
public string appauthor;//program author
... ...
}
}

At this point, you can now read the configuration element values, such as:

<!--sample1.aspx-->
private void Page_Load (object sender, System.EventArgs e)
{
Data data;
data = ConfigurationSettings.GetConfig ("MySection") as data;
This.tbName.Text = data. AppName;
}

Here we do the custom section by implementing a class that supports the IConfigurationSectionHandler interface to process the custom section. Of course, we can still directly declare that the system's handlers (System.Configuration.NameValueFileSectionHandler) reuse the same classes as appsettings.



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.