编程之路

——火地晋

  :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  291 随笔 :: 2 文章 :: 297 评论 :: 134万 阅读
参考:
http://www.cnblogs.com/wzh206/archive/2013/05/13/3076495.html

 

 web.config配置代码

 

复制代码
<?xml version="1.0" encoding="utf-8"?>

<!--
  有关如何配置 ASP.NET 应用程序的详细消息,请访问
  http://go.microsoft.com/fwlink/?LinkId=169433
  
-->

<configuration>
    <configSections>
        <section name="mailServerGroup" type="configSectionsDemo.Config.MailServerConfigurationHandler, configSectionsDemo, Version=1.0.0.0, Culture=neutral"  />
    </configSections>
    <mailServerGroup provider="www.edong.com">
        <mailServer client="forum.tracefact.net">
            <address>mail1.tracefact.net</address>
            <userName>jimmyzhang</userName>
            <password>123456</password>
        </mailServer>
        <mailServer client="blog.tracefact.com">
            <address>mail2.tracefact.net</address>
            <userName>webmaster</userName>
            <password>456789</password>
        </mailServer>
    </mailServerGroup>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>

</configuration>
复制代码

 

 类代码

 

MailServer

 

 

复制代码
using System.Collections;

namespace configSectionsDemo.Config
{
    public class MailServer
    {

        // 存储mailServer的子结点(Address,UserName,Password)的innerText值
        
// 以及属性 Client 的值
        private Hashtable serverNode;

        public MailServer()
        {
            serverNode = new Hashtable();
        }

        public Hashtable ServerNode
        {
            get { return serverNode; }
        }

        public string Client
        {
            get { return serverNode["client"as string; }
        }

        public string Address
        {
            get { return serverNode["address"as string; }
        }

        public string UserName
        {
            get { return serverNode["userName"as string; }
        }

        public string Password
        {
            get { return serverNode["password"as string; }
        }
    }
}
复制代码

 

MailServerConfig

 

 

复制代码
using System.Collections.Generic;

namespace configSectionsDemo.Config
{
    public class MailServerConfig : List<MailServer>
    {
        private string provider;     // 对应 mailServerGroup 结点的provider 属性
        public string Provider
        {
            get { return provider; }
            set { provider = value; }
        }
    }
}
复制代码

 

MailServerConfigurationHandler

 

复制代码
using System.Configuration;
using System.Xml;

namespace configSectionsDemo.Config
{
    // 自定义配置结点 mailServerGroup 的处理程序
    public class MailServerConfigurationHandler : IConfigurationSectionHandler
    {

        // section 为 MailServerGroup 结点
        public object Create(object parent, object configContext, XmlNode section)
        {

            // 设置方法返回的配置对象,可以是任何类型
            MailServerConfig config = new MailServerConfig();

            // 获取结点的属性信息       
            config.Provider =
                section.Attributes["provider"] == null ? "" : section.Attributes["provider"].Value;

            // 获取 MailServer 结点
            foreach (XmlNode child in section.ChildNodes)
            {

                MailServer server = new MailServer();

                // 添加Client属性
                if (child.Attributes["client"] != null)
                    server.ServerNode.Add("client", child.Attributes["client"].Value);

                // 获取MailServer下的 Name,UserName,Password 结点
                foreach (XmlNode grandChild in child.ChildNodes)
                {

                    // 添加文本
                    server.ServerNode.Add(grandChild.Name, grandChild.InnerText);
                }

                // 将server加入 MailServerConfig
                config.Add(server);
            }
            return config;
        }
    }
}
复制代码

页面代码

aspx

 

复制代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="configSectionsTest.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h1>
            使用 自定义结点 和 自定义处理程序</h1>
        <b>MailServerGroup Provider</b>:
        <asp:Literal ID="ltrServerProvider" runat="server"></asp:Literal>
        <h2>
            Mail Server1(Client:<asp:Literal ID="ltrClient1" runat="server"></asp:Literal>) Information:</h2>
        <hr />
        <b>Address</b>:
        <asp:Literal ID="ltrAddress1" runat="server"></asp:Literal>
        <br />
        <b>UserName</b>:
        <asp:Literal ID="ltrUserName1" runat="server"></asp:Literal><br />
        <b>Password</b>:
        <asp:Literal ID="ltrPassword1" runat="server"></asp:Literal>
        <h2>
            Mail Server2(Client:<asp:Literal ID="ltrClient2" runat="server"></asp:Literal>) Information:</h2>
        <hr />
        <b>Address</b>:
        <asp:Literal ID="ltrAddress2" runat="server"></asp:Literal>
        <br />
        <b>UserName</b>:
        <asp:Literal ID="ltrUserName2" runat="server"></asp:Literal><br />
        <b>Password</b>:
        <asp:Literal ID="ltrPassword2" runat="server"></asp:Literal>
        <br />
        <br />
    </div>
    </form>
</body>
</html>
复制代码

页面后端代码

 

复制代码
using System;
using System.Configuration;
using configSectionsDemo.Config;

namespace configSectionsTest
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        // SimpleCustom.aspx.cs
        protected void Page_Load(object sender, EventArgs e)
        {
            // 获取 mailServerConfig 对象
            MailServerConfig mailConfig = (MailServerConfig)ConfigurationManager.GetSection("mailServerGroup");

            // 获取 MailServerGroup 结点的 Provider 属性
            ltrServerProvider.Text = mailConfig.Provider;

            // 获取第一租 MailServer 数据
            ltrClient1.Text = mailConfig[0].Client;
            ltrAddress1.Text = mailConfig[0].Address;
            ltrUserName1.Text = mailConfig[0].UserName;
            ltrPassword1.Text = mailConfig[0].Password;

            // 获取第二租 MailServer 数据
            ltrClient2.Text = mailConfig[1].Client;
            ltrAddress2.Text = mailConfig[1].Address;
            ltrUserName2.Text = mailConfig[1].UserName;
            ltrPassword2.Text = mailConfig[1].Password;
        }
    }
}
复制代码

 

也是为了方便阅读参考,特简单摘录在此。代码一看就明白。

 

附上demo源码吧 ,一调试就明了。

https://files.cnblogs.com/yelaiju/configSectionsDemo.rar

 

 

 

 

 

posted on   火地晋  阅读(457)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示