WebConfigurationManager obtains custom nodes

Source: Internet
Author: User
-

Link: http://www.cnblogs.com/HeroBeast/archive/2008/09/27/1300614.html

 Abstract:

Most ASP.. NET applications contain many configuration information. Commonly, ConnectionString is configured as a connection string. When we need to use the Login control to automatically send an Email, We need to configure the Email server node; there are also many such as Page nodes that can introduce datasets and namespaces. According to my experience, many website initialization information (default settings) will be stored in the database, and many will also be saved in the configuration file, each of which has its own advantages. We can define our own nodes in web. config, and then read or update the custom configuration file. Of course, we can also customize a configuration file and parse it to read. This article mainly describes how to use some APIs provided by ASP. NET to customize configuration nodes in Web. config.

For better description, we first define a configuration node <NExplus/>, which has a property website name webName. You have your own Providers and a States node. The configuration is as follows:

<NExplus webName = "Custom configuration section in web. config">
<Providers>
<Add name = "CommonProvider" type = "NExplus. Data. SqlCommonProvider, NExplus. Data" connectionStringName = "SqlServer"/>
</Providers>
<States>
<Add name = "Xiamen" code = "0592"/>
<Add name = "Fuzhou" code = "0591"/>
<Add name = "Sanming" code = "0598"/>
</States>
</NExplus>

Read method:

The configuration file is an XML file. If it is a custom configuration file, you can use DotNet to read XML through serialization and deserialization, in a later version, you can also use XLinQ to read data. In the web. config file, you can use either of the following methods to read the configuration file:<configSections>Configure the associated operation class and its assembly in the node.

1. Define a class to implementSystem.Configuration.IConfigurationSectionHandlerInterface. This method is very easy to implement.

2. In ASP. NET2.0, we can easily operate nodes in the configuration file. It provides us with a series of classes. For example, if we define a class as long as we inheritConfigurationSectionThen we can define our nodes, and the child nodes can inheritConfigurationElementAnd so on. As long as you mark the attributeConfigurationPropertyAttribute.

Define the configuration class:

Public class CustomConfiguration: ConfigurationSection
{
[ConfigurationProperty ("webName", DefaultValue = "Custom configuration section in web. config", IsRequired = false)]
Public string WebName
{
Get
{
Return this ["webName"] as string;
}
}
}

The preceding only lists the methods used to read attributes.ConfigurationPropertyAttributeThe marked attributes can automatically instantiate the current class from the reverse sequence in the web. config file. Let's take a look.ConfigurationPropertyAttributeThe role of each attribute.

Name:The node name or attribute name must be unique.

DefaultValue: Default value for each attribute in the configuration file. If this attribute is not defined in the configuration file, this value is obtained when this attribute is called in the code.

IsRequired: Indicates whether the current attribute is required. If this attribute is true and is not defined in the configuration file, an error will be thrown when you access this website.

You can use System. Web. Configuration. WebConfigurationManager to obtain the instance of this node. The Code is as follows:

Public static CumstomConfiguration GetConfig (){
Return
WebConfigurationManager. GetSection ("NExplus") as CumstomConfiguration;
}

Read based on set configuration attributes:

Many times we need to get all the configurations of a set. The most common one is providers. The system has prepared the ProviderCollection for us and we just need to reference it directly. However, if you read the <states> node defined above, you need to write a set by yourself. This set must inheritConfigurationElementCollectionClass,The current object class must inheritConfigurationElementClass. To read<States> A node defines an object class as follows:

Public class ConfigurationState: ConfigurationElement
{
[ConfigurationProperty ("name", IsRequired = true)]
Public string Name
{
Get
{
Return this ["name"] as string;
}
}

[ConfigurationProperty ("code", IsRequired = false)]
Public string Code
{
Get
{
Return this ["code"] as string;
}
}
}

With the entity class, we certainly need to write a collection class. The Code is as follows: 

Public class ConfigurationStateCollection: ConfigurationElementCollection
{
Public ConfigurationState this [int index]
{
Get
{
Return base. BaseGet (index) as ConfigurationState;
}
Set
{
If (base. BaseGet (index )! = Null)
{
Base. BaseRemoveAt (index );
}
This. BaseAdd (index, value );
}
}

Protected override ConfigurationElement CreateNewElement ()
{
Return new ConfigurationState ();
}
 
Protected override object GetElementKey (ConfigurationElement element)
{
Return (ConfigurationState) element). Name;
}
}

 We need to read or set indexes for the current set, and rewrite two methods to get the current instance and the name of the current configuration node, of course, the last step is to add it to the configuration class (the secondary configuration class must be marked in the configSettings node ). The Code is as follows: 

Public class CustomConfiguration: ConfigurationSection
{
[ConfigurationProperty ("states")]
Public ConfigurationStateCollection CustomStates
{
Get
{
Return this ["states"] as ConfigurationStateCollection;
}
}
}

Summary:

This article is nothing new. Because I have been doing this recently, it is worth noting that in the providers application, we can read its set through ProvidersCollection, you can use ProvidersHelper to instantiate the current provider. If you inherit the ProviderBase class from your database provider abstract class, everything is okay. If you consider performance issues, you can use Cache to improve some performance. In the end, I hope to help more people and share my experience.

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.