Create custom configuration section handler in ASP. NET 3.5

Source: Internet
Author: User

In Asp.net, configuration data is stored in the web. config file. This file uses XML to represent data. All configuration information is located in And Between the root XML tags. The configuration information here is divided into two areas:Configuration section handler declaration area,Configuration section setting area.

The configuration section handler declaration area is located And Between XML tags, use the section element to declare the configuration section handler. You can nest these configuration section handler declarations in the sectiongroup element to help organize configuration information, as shown below:

  Configsections> sectiongroup name = "system. web. extensions "type =" system. web. configuration. systemwebextensionssectiongroup, system. web. extensions, version = 3.5.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35 "> sectiongroup name =" scripting "type =" system. web. configuration. scriptingsectiongroup, system. web. extensions, version = 3.5.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35 "> section name =" scriptresourcehandler "type =" system. web. configuration. scriptingscriptresourcehandlersection, system. web. extensions, version = 3.5.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35 "requirepermission =" false "allowdefinition =" machinetoapplication "/> sectiongroup name =" WebServices "type =" system. web. configuration. scriptingwebservicessectiongroup, system. web. extensions, version = 3.5.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35 "> section name =" jsonserialization "type =" system. web. configuration. scriptingjsonserializationsection, system. web. extensions, version = 3.5.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35 "requirepermission =" false "allowdefinition =" everywhere "/> section name =" profileservice "type =" system. web. configuration. scriptingprofileservicesection, system. web. extensions, version = 3.5.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35 "requirepermission =" false "allowdefinition =" machinetoapplication "/> section name =" authenticationservice "type =" system. web. configuration. scriptingauthenticationservicesection, system. web. extensions, version = 3.5.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35 "requirepermission =" false "allowdefinition =" machinetoapplication "/> section name =" roleservice "type =" system. web. configuration. scriptingroleservicesection, system. web. extensions, version = 3.5.0.0, culture = neutral, publickeytoken = token "requirepermission =" false "allowdefinition =" machinetoapplication "/> sectiongroup> configsections>
  

Each configuration section in the configuration section handler declaration area has a section handler declaration, which implements the. NET Framework class of the configurationsection class. The Section handler Declaration contains the section name (name) and the section handler Class Name (type) used to process the configuration data in this section ). As follows:

Section name = "jsonserialization" type = "system. web. configuration. scriptingjsonserializationsection, system. web. extensions, version = 3.5.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35 "requirepermission =" false "allowdefinition =" everywhere "/>

When you need to customize the configuration section, you need to complete the following tasks:

  1. Design your own configuration section handler class, implement the configurationsection class, and expand the functions you need;
  2. Declare the section handler class in the configuration section handler declaration area;
  3. In the configuration section, configure custom configurations.

1. Implement the configurationsection class and extend the functions you need

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Configuration;namespace EveryDayStudy{    public class CustomSection : ConfigurationSection    {        public CustomSection()        {        }        [ConfigurationProperty("CustomAttribute")]        public string CustomAttribute        {            get            {                return (String)this["CustomAttribute"];            }            set            {                this["CustomAttribute"] = value;            }        }    }}

Only one customattribute attribute is added here.

By modifying a common property (property) customattribute of a class, it becomes a configuration section attribute, which is implemented through configurationpropertyattribute.

2. Declare the section handler class in the configuration section handler declaration Area

  configSections>      section name="mySections" type="EveryDayStudy.CustomSection"/>  configSections>

3. In the configuration section, configure the custom configuration section.

mySections CustomAttribute="MyCustomAttribute">mySections>

Complete. Next let's run it in the. CS file.

CustomSection mySection =  (CustomSection)ConfigurationManager.GetSection("mySections");Response.Write("CustomAttribute Value Is " + mySection.CustomAttribute);

Run. Check the effect.

CustomAttribute Value Is MyCustomAttribute
 
Now, the mysections element is only one element. What if we want the newsection element to include child elements? As follows:
mySections CustomAttribute="MyCustomAttribute">  SubElement ElementAttribute="MyElementAttribute">SubElement>mySections>

To add a child element to the mysections element, we need to improve our custom configuration section program class, as shown below:

Using system; using system. collections. generic; using system. LINQ; using system. web; using system. configuration; namespace everydaystudy {public class customsection: configurationsection {public customsection () {} [configurationproperty ("customattribute")] Public String customattribute {get {return (string) this ["customattribute"];} set {This ["customattribute"] = value;} [configurationproperty ("Subelement")] public customelement subelement {// newly added attribute, because its return type inherits from configurationelement, // It can be used as a sub-element of the configuration section in Web. config. Get {return (customelement) This ["subelement"];} set {This ["subelement"] = value ;}} public class customelement: configurationelement {// newly added custom element [configurationproperty ("elementattribute")] Public String elementattribute {get {return (string) This ["elementattribute"];} set {This ["elementattribute"] = value ;}}}}

 

Access in The. CS file:

       CustomSection mySection =  (CustomSection)ConfigurationManager.GetSection("mySections");       Response.Write("CustomAttribute Value Is " + mySection.CustomAttribute);       Response.Write("ElementAttribute Value Is " + mySection.SubElement.ElementAttribute);

Run to see the effect:

Customattribute value is mycustomattribute

Elementattribute value is myelementattribute

Reference original

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.