参考:页面代码
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>
<!--
有关如何配置 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; }
}
}
}
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; }
}
}
}
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;
}
}
}
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>
<!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;
}
}
}
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
作者:
火地晋
出处: http://yelaiju.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处: http://yelaiju.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 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的设计模式综述