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