ASP. NET custom application configuration

Source: Internet
Author: User

1. The program calls ConfigurationManager. GetSection (sectionName)

2. Handler implements the Create (object parent, object configContext, XmlNode section) method of IConfigurationSectionHandler.

3. parse attribute & subNode of section through section XmlNode

 

Running effect:

The Code is as follows:

1. Call Method

Protected void Page_Load (object sender, EventArgs e) {// get ConfigManager-type instance ConfigManager config = (ConfigManager) ConfigurationManager. getSection ("traceFact"); ltrName. text = config. forumConfig. name; ltrOfflineTime. text = config. forumConfig. offlineTime. toString (); ltrPageSize. text = config. forumConfig. pageSize. toString (); ltrReplyCount. text = config. forumConfig. replyCount. toString (); ltrRootUrl. text = config. forumConfig. rootUrl. toString ();}

2. Web. Config

<Section name = "traceFact" type = "CustomConfig. generalConfigurationHandler, CustomConfig "/> <traceFact type =" CustomConfig. configManager, CustomConfig "> <forum name =" TraceFact. net Community "> <root url =" http://forum.tracefact.net "/> <replyCount> 20 </replyCount> <pageSize> 30 </pageSize> <offlineTime> 20 </offlineTime> </forum> <blog name = "JimmyZhang's Space"> <root url = "http://blog.tracefact.net"/> <urlMappings> <r EwriteRule> <request> ~ /(\ D {4})/Default \. aspx </request> <sendTo> ~ /BlogDetail. aspx? Year = $1 </sendTo> </rewriteRule> </urlMappings> </blog> <! -- Include the preceding configuration --> <mailServer address = "mail.tracefact.net" userName = "jimmyzhang" password = "123456"/> <greetingStrategy type = "ClassLib. chineseGreeting, ClassLib "/> </traceFact>

 

3. Handler code

Public class GeneralConfigurationHandler: IConfigurationSectionHandler {// The section node here is Web. greetingStrategy node public object Create (object parent, object configContext, XmlNode section) in Config {// obtain the value of the node type attribute Type t = Type. getType (section. attributes ["type"]. value); // directly pass the section to object [] parameters = {section}; // type instance to be created object obj = null; try {obj = Activator. createInstance (t, parameters); // use a constructor with parameters} catch (Exception ex) {return null;} // obj is the object defined in the type attribute of the node, here is ClassLib. chineseGreetingreturn obj ;}} public class ConfigManager {private XmlNode section; private ForumConfiguration forumConfig; // private BlogConfiguration blogConfig; // private MailServerConfiguration mailServerConfig; // private IGreetingStrategy greetingStrategy; // similar to the following, omitted... public ForumConfiguration ForumConfig {get {return forumConfig; }}// public BlogConfiguration BlogConfig {// get {return blogConfig;} //} public ConfigManager (XmlNode section) {this. section = section; forumConfig = new ForumConfiguration (section. selectSingleNode ("forum"); // blogConfig = new BlogConfiguration (section. selectSingleNode ("blog"); // mailServerConfig = new MailServerConfiguration (section. selectSingleNode ("mailServer"); // similar to the following, omitted ...}} // specific sub-node configuration forum node public class ForumConfiguration {private XmlNode forumNode; // pass the forum node in public ForumConfiguration (XmlNode section) {this. forumNode = section;} public string Name {get {return forumNode. attributes ["name"]. value ;}} public string RootUrl {get {return forumNode. selectSingleNode ("root "). attributes ["url"]. value ;}} public int PageSize {get {return int. parse (forumNode. selectSingleNode ("pageSize "). innerText) ;}} public int ReplyCount {get {return int. parse (forumNode. selectSingleNode ("replyCount "). innerText) ;}} public int OfflineTime {get {return int. parse (forumNode. selectSingleNode ("offlineTime "). innerText );}}}
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.