Web. config Custom node configsections

Source: Internet
Author: User
Tags configuration settings

Original address: http://www.cnblogs.com/huc87/archive/2009/05/06/1450981.html

1. Why a custom node is required

In order to increase the portability of the application, usually the site needs to configure some custom nodes, such as: File upload path, and then in-depth application, you can define the factory method needs to create the class.

2.configSections How to use

Defining a custom node under the configsections node can help us implement our own nodes.

Define your own node first, and define the method as follows:

<configSections>
<sectiongroup name= "section group name" >
<section name= "section name" type= "configuration section handler class"/>
</sectionGroup>
</configSections>

The node that defines itself must be on the configsections node.

The sectiongroup element acts as a container for the section element.

The section element associates a configuration stanza handler with a configuration element or section. Because ASP. NET does not make any assumptions about how to handle the settings within the configuration file, this is necessary. However, ASP. NET will delegate processing of configuration data to the configuration section handler, as described later. Each section element identifies one of the configuration sections or elements. You can logically group the section elements in the sectiongroup element to organize the section elements and avoid naming conflicts. The section and sectiongroup elements are included in the configsections element.

Sectiongroup Node Properties:

Name: The required String property, which is the name that the group element uses in the section Settings area of the configuration file.

Section Node Properties:

Name: The required String property that specifies the name of the configuration section or element associated with the configuration section handler specified in the Type property. This is the name that the element uses in the section Settings area of the configuration file.

Type: The required String property that specifies the name of the configuration section handler class that is used to handle the configuration settings in the section or element specified in the Name property.

Now that you have defined your own node, you can use that node as well. Here's how to use it:

<section Group Name>
<section name>
<add key= "Key1" value= "value1"/>
</section name>
</section Group Name>

Define your own node, how to read the node information?

The following is the exact words on MSDN:

You can extend the standard set of ASP. NET configuration settings with your own XML configuration elements. To complete the operation, you must create your own configuration section handler.

The handler must be an implementation of the System.Configuration.IConfigurationSectionHandler interface or the System.Configuration.ConfigurationSection class. NET Framework class.

The section handler interprets and processes the settings defined in the XML configuration element in a specific part of the Web. config file, and returns the appropriate configuration object based on the configuration settings. The configuration object returned by the handler class can be any data structure; it is not limited to any base configuration class or configuration format. ASP. NET uses this configuration object to read and write to the custom configuration element.

The above paragraph means that we are going to define a class that inherits from the System.Configuration.IConfigurationSectionHandler interface or System.Configuration.ConfigurationSection class.

This class is then used to process our custom nodes.

We see that there is only one method in the System.Configuration.IConfigurationSectionHandler interface:

Create a configuration section handler
Object Create (object parent, Object configcontext, XmlNode section)

return value

Creates a section handler object.

What is this class for? Let's take a look at an example.

First, we create a new Web site project and add the following nodes to the Web. config:

<configSections>
<sectiongroup name= "Websiteinfo" >
<section name= "Basicinfo" type= "Configurationsectiontest.websiteinfohandler"/>
<section name= "FileUpload" type= "Configurationsectiontest.websiteinfohandler"/>
</sectionGroup>
</configSections>

<WebSiteInfo>
<basicInfo>
<add key= "name" value= "Huchen ' s homepage"/>
<add key= "Version" value= "1.0"/>
</basicInfo>
<fileUpload>
<add key= "Fileuploadpath" value= "e:\\myhomepage\\web\\upload\\"/>
<add key= "Fileuploadsizemax" value= "2M"/>
</fileUpload>
</WebSiteInfo>

Above we have defined two nodes Basicinfo and fileupload under the Websiteinfo node, The node handler class Configurationsectiontest.websiteinfohandler is defined and then the nodes we define are used.

Let's take a look at the node handler Configurationsectiontest.websiteinfohandler.

Create a project arbitrarily, create a new class, or create a new class directly in App_Code, as follows:

and set a breakpoint in the CREATE function.

Namespace Configurationsectiontest
{
<summary>
Summary description of Websiteinfohandler
</summary>
public class Websiteinfohandler:iconfigurationsectionhandler
{
Public Websiteinfohandler ()
{
//
TODO: Add constructor logic here
//

}

#region IConfigurationSectionHandler Members

public Object Create (object parent, Object configcontext, System.Xml.XmlNode section)
{
Here we first return a Hello, and set a breakpoint here. See when the program executes to this.
return "Hello";
}

#endregion
}

}

Then go to our custom node in Default.aspx's Page_Load event handler, and in ConfigurationSettings.GetConfig ("Websiteinfo/basicinfo"); The breakpoint is set on this statement.

protected void Page_Load (object sender, EventArgs e)
{
Object o = ConfigurationSettings.GetConfig ("Websiteinfo/basicinfo");
}

Well, we start debugging and see that the program executes first to ConfigurationSettings.GetConfig ("Websiteinfo/basicinfo");

Then execute to the CREATE function in Configurationsectiontest.websiteinfohandler.

Let's take a look at the values of the parameters in the processing function:

Parent is null

Configcontext is the configuration context object.

Section InnerXml is <add key= "name" value= "Huchen ' homepage"/><add key= "version" value= "1.0"/>

Press F11 to continue with return "Hello" and Continue execution ...

After executing to "}" after the object o = ConfigurationSettings.GetConfig ("Websiteinfo/basicinfo"), we find that the value of O is "hello".

I'm sure you know that. When you read the contents of a custom node, the program executes the node handler that we defined and passes the contents of the node to the parameters in the CREATE function. We then process the content under the node in Create and return the content of our formatted node to the caller, the ConfigurationSettings.GetConfig ("Websiteinfo/basicinfo").

Well, once we know this, we'll refine our code, and we'll handle the incoming node content in Create.

For the sake of simplicity, we introduce two classes of Namevaluecollection,namevaluesectionhandler.

NameValueCollection: Represents a collection of associative string keys and string values that can be accessed through a key or index.

NameValueSectionHandler: Provides configuration information for name/value pairs in the configuration section. NameValueSectionHandler This class also inherits IConfigurationSectionHandler
The anti-compilation can see that the NameValueSectionHandler create method transforms the result of the parameter section into a set, or NameValueCollection.

Then we can write the following code in the CREATE function in the node handler:

NameValueCollection configs;
NameValueSectionHandler Basehandler = new NameValueSectionHandler ();
Configs = (NameValueCollection) basehandler.create (parent,configcontext,section);
Return configs;

So we can access our nodes in this way:

String mywebsitename = ((NameValueCollection) configurationsettings.getconfig ("Websiteinfo/basicinfo")) ["Name"];

Add the following code to the Default.aspx Page_Load event handler:

String mywebsitename = ((NameValueCollection) configurationsettings.getconfig ("Websiteinfo/basicinfo")) ["Name"];
Response.Write (Mywebsitename);

Ctrl+f5 run, you can see the page output Huchen ' s homepage

Web. config Custom node configsections

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.