[Original] custom Config example, using the IConfigurationSectionHandler Interface

Source: Internet
Author: User
1. MasterPage
Call Method
Private void GetConfig ()
{
NameValueCollection channelCollection = new CooperationChannelsConfig (). Settings;

Foreach (String channel in channelCollection)
{
If (channel. Equals (UIHelper. GetServerName (this. Page )))
{
DomainName = channelCollection [channel]. Split ('|') [0]. ToString ();
This. Page. Title = channelCollection [channel]. Split ('|') [1]. ToString ();
Break;
}
}
}

2. CooperationChannelsConfig:
// Implement a class that supports the IConfigurationSectionHandler interface to process custom sections and read them.
Namespace Config
{
Using System;
Using System. Data;
Using System. Configuration;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Using System. Collections. Specialized;
Using System. Xml;

Public class CooperationChannelsConfig: IConfigurationSectionHandler
{
Public object Create (object parent, object configContext, XmlNode section)
{
NameValueCollection settings;

Try
{
NameValueSectionHandler baseHandler = new NameValueSectionHandler ();
Settings = (NameValueCollection) baseHandler. Create (parent, configContext, section );
}
Catch
{
Settings = null;
}

Return settings;
}

/** // <Summary>
/// Return the entire Channel
/// </Summary>
Public NameValueCollection Settings
{
Get
{
Return (NameValueCollection) ConfigurationManager. GetSection ("channel ");
}
}
}
}

3. Create a Global file in the project and add a sentence in the Application_Start () event to get the GetSection:
System. Configuration. ConfigurationManager. GetSection ("channel ");

4. Configure the mapping class and the custom config file used only in Web. config:
<Section name = "channel" type = "Config. CooperationChannelsConfig, Config"/>
<Channel configSource = "Config \ Channel. config"/>

5. Channel. config: Custom Config File

<? Xml version = "1.0" encoding = "gb2312"?>
<Channel>
<Add key = "Key" value = "value"/>
<Add key = "Key1" value = "value1"/>
</Channel>

Finally, I also provide an example of the custom configuration section above MSDN:
Ms-help: // MS. MSDNQTR. v80.chs/MS. MSDN. v80/MS. VisualStudio. v80.chs/dv_aspnetconfig/html/07f68a3f-2920-4665-a316-47bda744e662.htm

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.