ASP.NET配置文件中自定義節(jié)點(diǎn)
節(jié)處理程序解釋并處理 Web.config 文件特定部分中 XML 配置元素中定義的設(shè)置,并根據(jù)配置設(shè)置返回適當(dāng)?shù)呐渲脤?duì)象。
處理程序類返回的配置對(duì)象可以是任何數(shù)據(jù)結(jié)構(gòu);它不限于任何基配置類或配置格式。
ASP.NET 使用該配置對(duì)象,以對(duì)自定義配置元素進(jìn)行讀取和寫入。
1、創(chuàng)建自定義配置節(jié)處理程序
創(chuàng)建一個(gè)繼承 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 配置文件添加自定義節(jié)處理程序
<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、以編程方式訪問自定義配置數(shù)據(jù)
獲取自定義配置對(duì)象的一個(gè)實(shí)例,并使用 GetSection 方法或 GetSection 方法來填充該實(shí)例。
下面的 ASPX 頁(yè)的示例使用前一個(gè)示例,以枚舉自定義配置節(jié)的屬性和子元素。
<%@ 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>到此這篇關(guān)于ASP.NET配置文件中自定義節(jié)點(diǎn)的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
在ASP.NET?MVC下限制同一個(gè)IP地址單位時(shí)間間隔內(nèi)的請(qǐng)求次數(shù)的解決方法
有時(shí)候,當(dāng)用戶請(qǐng)求一個(gè)Controller下的Action,我們希望,在單位時(shí)間間隔內(nèi),比如每秒,每分鐘,每小時(shí),每天,每星期,限制同一個(gè)IP地址對(duì)某個(gè)Action的請(qǐng)求次數(shù),如何做呢?這篇文章主要介紹了在ASP.NET?MVC下限制同一個(gè)IP地址單位時(shí)間間隔內(nèi)的請(qǐng)求次數(shù),需要的朋友可以參考下2024-01-01
ASP.NET?Core使用EF創(chuàng)建關(guān)系模型
這篇文章介紹了ASP.NET?Core使用EF創(chuàng)建關(guān)系模型的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04
.Net Core路由處理的知識(shí)點(diǎn)與方法總結(jié)
這篇文章主要給大家介紹了關(guān)于.Net Core路由處理的知識(shí)點(diǎn)與方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
asp.net開發(fā)中常見公共捕獲異常方式總結(jié)(附源碼)
這篇文章主要介紹了asp.net開發(fā)中常見公共捕獲異常方式總結(jié),結(jié)合實(shí)例形式較為詳細(xì)的分析了asp.net捕獲異常的相關(guān)技巧,并提供了完整的實(shí)例代碼供讀者下載參考,需要的朋友可以參考下2015-11-11
asp.net使用AJAX實(shí)現(xiàn)無刷新分頁(yè)
AJAX(Asynchronous JavaScript and XML)是一種進(jìn)行頁(yè)面局部異步刷新的技術(shù)。用AJAX向服務(wù)器發(fā)送請(qǐng)求和獲得服務(wù)器返回的數(shù)據(jù)并且更新到界面中,不是整個(gè)頁(yè)面刷新,而是在頁(yè)面中使用Js創(chuàng)建XMLHTTPRequest對(duì)象來向服務(wù)器發(fā)出請(qǐng)求以及獲得返回的數(shù)據(jù)。2014-11-11
.NET事件監(jiān)聽機(jī)制的局限與擴(kuò)展分析
這篇文章主要介紹了.NET事件監(jiān)聽機(jī)制的局限與擴(kuò)展,詳細(xì)分析了.NET事件監(jiān)聽機(jī)制的機(jī)制與優(yōu)劣,有助于更好的理解.NET的運(yùn)行原理,需要的朋友可以參考下2014-11-11

