網站首頁 編程語言 正文
節處理程序解釋并處理 Web.config 文件特定部分中 XML 配置元素中定義的設置,并根據配置設置返回適當的配置對象。
處理程序類返回的配置對象可以是任何數據結構;它不限于任何基配置類或配置格式。
ASP.NET 使用該配置對象,以對自定義配置元素進行讀取和寫入。
1、創建自定義配置節處理程序
創建一個繼承?System.Configuration.ConfigurationSection?類的公共類
public class MyHandler : ConfigurationSection
{
public MyHandler()
{
}
public MyHandler(String attribVal)
{
MyAttrib1 = attribVal;
}
[ConfigurationProperty("myAttrib1", DefaultValue = "Clowns", IsRequired = true)]
[StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)]
public String MyAttrib1
{
get
{ return (String)this["myAttrib1"]; }
set
{ this["myAttrib1"] = value; }
}
[ConfigurationProperty("myChildSection")]
public MyChildConfigElement MyChildSection
{
get
{ return (MyChildConfigElement)this["myChildSection"]; }
set
{ this["myChildSection"] = value; }
}
}
public class MyChildConfigElement : ConfigurationElement
{
public MyChildConfigElement()
{
}
public MyChildConfigElement(String a1, String a2)
{
MyChildAttribute1 = a1;
MyChildAttribute2 = a2;
}
[ConfigurationProperty("myChildAttrib1", DefaultValue = "Zippy", IsRequired = true)]
[StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)]
public String MyChildAttribute1
{
get
{ return (String)this["myChildAttrib1"]; }
set
{ this["myChildAttrib1"] = value; }
}
[ConfigurationProperty("myChildAttrib2", DefaultValue = "Michael Zawondy", IsRequired = true)]
[StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)]
public String MyChildAttribute2
{
get
{ return (String)this["myChildAttrib2"]; }
set
{ this["myChildAttrib2"] = value; }
}
}
2、向 ASP.NET 配置文件添加自定義節處理程序
<configuration> <configSections> <sectionGroup name="myCustomGroup"> <section name="myCustomSection" type="MyConfigSectionHandler.MyHandler, MyCustomConfigurationHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" allowLocation="true" allowDefinition="Everywhere" /> </sectionGroup> </configSections> <myCustomGroup> <myCustomSection myAttrib1="Clowns"> <myChildSection myChildAttrib1="Zippy" myChildAttrib2="Michael Zawondy "/> </myCustomSection> </myCustomGroup> </configuration>
3、以編程方式訪問自定義配置數據
獲取自定義配置對象的一個實例,并使用?GetSection?方法或?GetSection?方法來填充該實例。
下面的 ASPX 頁的示例使用前一個示例,以枚舉自定義配置節的屬性和子元素。
<%@ Page Language="C#" %>
<script runat="server">
protected void Button1_Click(object sender, EventArgs e)
{
MyConfigSectionHandler.MyHandler config =
(MyConfigSectionHandler.MyHandler)System.Configuration.ConfigurationManager.GetSection(
"myCustomGroup/myCustomSection");
StringBuilder sb = new StringBuilder();
sb.Append("<h2>Attributes in the myCustomSection Element:</h2>");
sb.AppendFormat("myAttrib1 = {0}<br/>", config.MyAttrib1.ToString());
sb.Append("<h2>Attributes in the myChildSection Element:</h2>");
sb.AppendFormat("myChildAttrib1 = {0}<br/>", config.MyChildSection.MyChildAttribute1.ToString());
sb.AppendFormat("myChildAttrib2 = {0}<br/>", config.MyChildSection.MyChildAttribute2.ToString());
Label1.Text = sb.ToString();
Label1.Visible = true;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Enumerate MyCustomSection</h1>
<asp:Label ID="Label1" runat="server"
Text="" />
<br />
<asp:Button ID="Button1" runat="server"
Text="Get Custom Config Info"
OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
原文鏈接:https://www.cnblogs.com/springsnow/p/9399286.html
相關推薦
- 2023-07-25 node項目使用crypto模塊為用戶密碼加密
- 2022-06-26 python如何利用matplotlib繪制并列雙柱狀圖并標注數值_python
- 2022-03-31 C語言16進制與ASCII字符相互轉換_C 語言
- 2022-06-25 iOS使用UICollectionView實現拖拽移動單元格_IOS
- 2023-07-07 electron 運行以及打包超時解決方法
- 2021-12-08 linux中grub啟動引導程序的加密介紹_Linux
- 2021-11-26 Redis分布式鎖詳細介紹_Redis
- 2023-04-01 sqlserver字符串拼接的實現_MsSql
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支