博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

ASP.NET 通用自定义应用程序配置

Posted on 2011-02-11 21:44  飞鸟123  阅读(380)  评论(0编辑  收藏  举报

1.  程序调用ConfigurationManager.GetSection(sectionName)

2.  Handler 实现IConfigurationSectionHandler 的 Create(object parent, object configContext, XmlNode section) 方法

3.  通过section XmlNode 解析 section 的 attribute & subNode

 

运行效果:

image

代码如下:

1. 调用方法

protected void Page_Load(object sender, EventArgs e) {

		// 获取 ConfigManager 类型实例
		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>
			<rewriteRule>
				<request>~/(\d{4})/Default\.aspx</request>
				<sendTo>~/BlogDetail.aspx?year=$1</sendTo>
			</rewriteRule>
		</urlMappings>
	</blog>

	<!-- 将上面的配置也包含进来 -->
	<mailServer address="mail.tracefact.net" userName="jimmyzhang" password="123456" />
	<greetingStrategy type="ClassLib.ChineseGreeting, ClassLib" />
</traceFact>

 

3. Handler 代码

	public class GeneralConfigurationHandler : IConfigurationSectionHandler {
		// 这里的section结点为 Web.Config中的greetingStrategy结点
		public object Create(object parent, object configContext, XmlNode section) {

			// 获取结点type属性的值
			Type t = Type.GetType(section.Attributes["type"].Value);

			// 直接将section进行传递
			object[] parameters = { section };

			// 将要创建的类型实例
			object obj = null;

			try {
				obj = Activator.CreateInstance(t, parameters);	// 使用有参数的构造函数
			} catch (Exception ex) {
				return null;
			}
			// obj为结点的 type属性中定义的对象,在这里是 ClassLib.ChineseGreeting
			return obj;
		}
	}


	public class ConfigManager {

		private XmlNode section;
		private ForumConfiguration forumConfig;

		// private BlogConfiguration blogConfig;
		// private MailServerConfiguration mailServerConfig;
		// private IGreetingStrategy greetingStrategy;
		// 以下类似,省略 ...


		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"));
			// 以下类似,省略 ...
		}
	}

	// 具体的子结点配置 forum 结点
	public class ForumConfiguration {
		private XmlNode forumNode;

		// 将 forum 结点传递进来
		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); }
		}
	}