• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

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

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

 

运行效果:

代码如下:

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); }
		}
	}

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Asp.Net基础-4.ASP.Net揭秘之Input版自增+5.ViewState初探发布时间:2022-07-10
下一篇:
ASP.Net设置404错误跳转到指定页面发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap