Adding a custom configuration to Web. config using IConfigurationSectionHandler

Source: Internet
Author: User

A. Scene
Here is an example of a simple application where I want to add basic information about the site in Web. config, such as: Website name, website version number, whether the site is temporarily closed, etc.
Two. Basic implementation methods
1. Define the class that corresponds to the configuration node: sitesetting
Code snippet:

Namespace Tristan.seecustomconfig {
public class Sitesetting {
public string SiteName {get; set;}
public string Siteversion {get; set;}
public bool Closed {get; set;}
}
}


2. Implement IConfigurationSectionHandler Interface: Sitesettinghandler

Namespace Tristan.seecustomconfig {
public class Sitesettinghandler:iconfigurationsectionhandler {
#region IConfigurationSectionHandler Members

public object "Create" (object parent, Object configcontext, System.Xml.XmlNode section) {
String siteName = section. selectSingleNode ("SiteName"). InnerText;
String Siteversiton = section. selectSingleNode ("Siteversion"). InnerText;
BOOL closed = Convert.toboolean (section. selectSingleNode ("closed"). InnerText);
return new sitesetting () {SiteName = SiteName, siteversion = Siteversiton};
}

#endregion
}
}

3. Configuring in Web. config
Add a node to the <configSections></configSections>:
<section name= "sitesetting" type= "Tristan.SeeCustomConfig.SiteSettingHandler"/>

Name: Specifies that the node we are going to add is named "Sitesetting", which is then used to write the configuration node
Type: Specifies the handler that handles this configuration node, this class, we have already implemented the code in the previous
Then add a section of XML to the <configuration><configuration>:
<siteSetting>
<siteName> Meet the Future </siteName>
<siteVersion>1.0</siteVersion>
<closed>false</closed>
</siteSetting>


4. Look at the effect.
Just build a page in the background code to write a few lines of code to do a test:

Namespace Tristan.seecustomconfig {
Public partial class _default:system.web.ui.page {
protected void Page_Load (object sender, EventArgs e) {
sitesetting site = configurationmanager.getsection ("sitesetting") as sitesetting;
Response.Write (site. SiteName + "," + site. Siteversion + "," + site. Closed.tostring ());
}
}
}

Run, you can see that our information in the Web. config is being write out. :)
Three. Deserializing using XML
1. Modify Sitesetting

Namespace Tristan.seecustomconfig {

[Serializable]
[XmlRoot ("sitesetting")]
public class Sitesetting {
[XmlElement ("SiteName", typeof (String))]
public string SiteName {get; set;}

[XmlElement ("Siteversion", typeof (String))]
public string Siteversion {get; set;}

[XmlElement ("Closed", typeof (Boolean))]
public bool Closed {get; set;}
}
}


2. Modify Sitesettinghandler

Namespace Tristan.seecustomconfig {
public class Sitesettinghandler:iconfigurationsectionhandler {
#region IConfigurationSectionHandler Members

public object "Create" (object parent, Object configcontext, System.Xml.XmlNode section) {
String siteName = section. selectSingleNode ("SiteName"). InnerText;
String Siteversiton = section. selectSingleNode ("Siteversion"). InnerText;
BOOL closed = Convert.toboolean (section. selectSingleNode ("closed"). InnerText);
return new sitesetting () {SiteName = SiteName, siteversion = Siteversiton};

String typeName = ((XmlElement) section). GetAttribute ("type");
XmlSerializer XZ = new XmlSerializer (Type.GetType (typeName));
using (StringReader sr = new StringReader (section. OuterXml)) {
Return XZ. Deserialize (SR);
}
}

#endregion
}
}


3. Modify the configuration in Web. config

<sitesetting type= "Tristan.SeeCustomConfig.SiteSetting" >
<siteName> Meet the Future </siteName>
<siteVersion>1.0</siteVersion>
<closed>false</closed>
</siteSetting>


4. Take a look again.
Do not modify the test code, get the same effect:)

Adding a custom configuration to Web. config using IConfigurationSectionHandler

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.