日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

ASP.NET杂谈-一切都从web.config说起(2)(ConfigSections详解-上 )

發(fā)布時間:2025/5/22 asp.net 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ASP.NET杂谈-一切都从web.config说起(2)(ConfigSections详解-上 ) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

ConfigSections的結(jié)構(gòu)


首先我們先回顧一下ConfigSections的結(jié)構(gòu)和它子節(jié)點(diǎn)的說明,如下:

1: <configSections> 2: <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> 3: <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> 4: <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" 5: requirePermission="false" allowDefinition="MachineToApplication"/> 6: <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> 7: <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" 8: requirePermission="false" allowDefinition="Everywhere" /> 9: <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" 10: requirePermission="false" allowDefinition="MachineToApplication" /> 11: <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" 12: requirePermission="false" allowDefinition="MachineToApplication" /> 13: <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" 14: requirePermission="false" allowDefinition="MachineToApplication" /> 15: </sectionGroup> 16: </sectionGroup> 17: </sectionGroup> 18: </configSections>

?

ConfigSectins屬性和子節(jié)點(diǎn)說明????


屬性:

??? 無。

子節(jié)點(diǎn)說明:

節(jié)點(diǎn)名稱

功能描述

sectionGroup

定義配置節(jié)處理程序與配置節(jié)之間的關(guān)聯(lián)。

section

定義配置節(jié)處理程序與配置元素之間的關(guān)聯(lián)。

我們不難發(fā)現(xiàn)ConfigSectings主要包含SectiongGroup和Section兩個子節(jié)點(diǎn),下面就介紹一下這兩個節(jié)點(diǎn)的屬性說明:

1、sectionGroup屬性說明

?


屬性名稱

功能描述

name

指定與下面?type?屬性指定的配置節(jié)處理程序關(guān)聯(lián)的配置節(jié)或元素的名稱。

type

指定用來執(zhí)行如下操作的配置節(jié)處理程序類的名稱:處理在?name?屬性中指定的節(jié)或元素中的配置設(shè)置。使用以下格式:

type="?Fully qualified class name?,?assembly file name?,?version?,?culture?,?public key token?",定義必須匹配程序集引用。

程序集文件必須與定義它的 Web.config 文件位于同一個應(yīng)用程序目錄中。

SectionGroup中還是可以在包含多個SectionGroup和Section。

?

?

2、section屬性說明


屬性名稱

功能描述

name

指定與 type 屬性中指定的配置節(jié)處理程序關(guān)聯(lián)的配置節(jié)或元素的名稱。

type

指定用來執(zhí)行如下操作的配置節(jié)處理程序類的名稱:處理在 name 屬性中指定的節(jié)或元素中的配置設(shè)置,格式和上面sectionGroup屬性中的type格式相同。

requirePermission

指定是否得到相關(guān)的配置部分要求存取權(quán)限信息。可選的 Boolean 屬性。

restartOnExternalChanges

指定在該節(jié)的配置數(shù)據(jù)發(fā)生更改時是否應(yīng)當(dāng)重新啟動應(yīng)用程序,不適用于 ASP.NET 應(yīng)用程序,可選的 Boolean 屬性。

allowLocation

指定是否可以在 location 元素內(nèi)使用該節(jié),僅適用于 ASP.NET 應(yīng)用程序,默認(rèn)值為 True。

allowExeDefinition

指定可以在哪個配置文件中使用該節(jié),僅適用于 .NET Framework 客戶端應(yīng)用程序,可選的 Boolean 屬性。

allowDefinition

指定可以在哪個配置文件中使用該節(jié),僅適用于 ASP.NET 應(yīng)用程序,可選的 Boolean 屬性。

其實(shí)在配置allowDefinition和allowExeDefinition屬性的時候,他們其實(shí)是有選擇值的。allowDefinition的值是在ConfigurationAllowDefinition?枚舉中選擇,

而allowExeDefinition的值是在ConfigurationAllowExeDefinition?枚舉中選擇。下面就介紹一下這兩個枚舉中各個值的的介紹:

1、ConfigurationAllowDefinition 枚舉


?

描述

Everywhere

允許在任何配置文件或目錄中配置該節(jié),如下所示:

  • Machine.config。

  • 根 Web.config。

  • 應(yīng)用程序的 Web.config。

  • 虛擬目錄。

  • 應(yīng)用程序中的物理子目錄。

如果未使用 allowDefinition 屬性,則假設(shè)為?Everywhere。這是默認(rèn)設(shè)置。

MachineToApplication

允許在下列文件之一中配置該節(jié):

  • Machine.config。

  • 根 Web.config。

  • 應(yīng)用程序的 Web.config。

這不包括位于應(yīng)用程序中的虛擬目錄或物理子目錄下的 Web.config 文件。

MachineToWebRoot

允許在下列文件之一中配置該節(jié):

  • Machine.config。

  • 根 Web.config。

MachineOnly

只允許在 Machine.config 文件中配置該節(jié)。

注釋:

????? Machine.config的位置:%SystemRoot%\Microsoft.NET\Framework\versionNumber\CONFIG 中。

????? 根 Web.config的位置:%SystemRoot%\Microsoft.NET\Framework\versionNumber\CONFIG 中。

2、ConfigurationAllowExeDefinition 枚舉


?

描述

MachineToApplication

可在 Machine.config 文件或客戶端應(yīng)用程序目錄中的 Exe.config 文件中定義 ConfigurationSection。

MachineToRoamingUser

可在 Machine.config 文件、客戶端應(yīng)用程序目錄中的 Exe.config 文件、漫游用戶目錄中的 User.config 文件或本地用戶目錄中的 User.config 文件中定義 ConfigurationSection。

MachineToLocalUser

可在 Machine.config 文件、客戶端應(yīng)用程序目錄中的 Exe.config 文件或漫游用戶目錄中的 User.config 文件中定義 ConfigurationSection

MachineOnly

ConfigurationSection 只能在 Machine.config 文件中定義。

舉例說明


?

上面介紹了這么多,下面就用兩個簡單的Demo程序來介紹一下他們的具體應(yīng)用吧:

1、Demo01介紹section的應(yīng)用。

2、Demo02介紹包括子元素section的應(yīng)用。

Demo01介紹一個配置用戶信息的Section

以下代碼是Section的結(jié)構(gòu)和屬性的定義。

1: using System; 2: using System.Collections.Generic; 3: using System.Linq; 4: using System.Text; 5: ? 6: using System.Configuration; 7: ? 8: namespace KevinDiao.MySectionDemo01 9: { 10: /// <summary> 11: /// 自定義Section的結(jié)構(gòu) 12: /// </summary> 13: public class MySection:ConfigurationSection 14: { 15: /// <summary> 16: /// 用戶名稱 17: /// </summary> 18: [ConfigurationProperty("username",IsRequired=true)] 19: public string UserName 20: { 21: get 22: { 23: return (string)this["username"]; 24: } 25: set 26: { 27: this["username"] = value; 28: } 29: } 30: /// <summary> 31: /// 用戶密碼 32: /// </summary> 33: [ConfigurationProperty("password", IsRequired = true)] 34: public string Password 35: { 36: get 37: { 38: return (string)this["password"]; 39: } 40: set 41: { 42: this["password"] = value; 43: } 44: } 45: } 46: }

下面介紹時簡單的獲取程序:

1: using System; 2: using System.Collections.Generic; 3: using System.Linq; 4: using System.Web; 5: using System.Web.UI; 6: using System.Web.UI.WebControls; 7: ? 8: using System.Configuration; 9: using KevinDiao.MySectionDemo01; 10: ? 11: namespace KevinDiao.AspNetDemo01 12: { 13: public partial class _Default : System.Web.UI.Page 14: { 15: protected void Page_Load(object sender, EventArgs e) 16: { 17: MySection mySection = (MySection)ConfigurationManager.GetSection("MySectionHandle01"); 18: Response.Write("UserName:"+mySection.UserName+"<br/>"); 19: Response.Write("Password:"+mySection .Password); 20: } 21: } 22: }

web.config中的配置信息

1: <configSections> 2: <section name="MySectionHandle01" type="KevinDiao.MySectionDemo01.MySection,KevinDiao.MySectionDemo01"/> 3: </configSections> 4: <MySectionHandle01 username="kevindiao" password="123456"></MySectionHandle01>

?

獲取到的結(jié)果:

UserName:kevindiao Password:123456

Demo02還是用用戶信息來舉例吧,以下是具有的代碼:

以下是自定義Section的結(jié)構(gòu):

1: using System; 2: using System.Collections.Generic; 3: using System.Linq; 4: using System.Text; 5: ? 6: using System.Configuration; 7: ? 8: namespace KevinDiao.MySectionDemo02 9: { 10: /// <summary> 11: /// 自定義Section 12: /// </summary> 13: public class MySectionHandle:ConfigurationSection 14: { 15: [ConfigurationProperty("users",IsRequired=true)] 16: public MySectionElement Users 17: { 18: get 19: { 20: return (MySectionElement)this["users"]; 21: } 22: } 23: } 24: }

自定義Element:

1: using System; 2: using System.Collections.Generic; 3: using System.Linq; 4: using System.Text; 5: ? 6: using System.Configuration; 7: ? 8: namespace KevinDiao.MySectionDemo02 9: { 10: /// <summary> 11: /// 自定義Element 12: /// </summary> 13: public class MySectionElement : ConfigurationElement 14: { 15: /// <summary> 16: /// 用戶名 17: /// </summary> 18: [ConfigurationProperty("username", IsRequired = true)] 19: public string UserName 20: { 21: get 22: { 23: return (string)this["username"]; 24: } 25: } 26: /// <summary> 27: /// 密碼 28: /// </summary> 29: [ConfigurationProperty("password", IsRequired = true)] 30: public string Password 31: { 32: get 33: { 34: return (string)this["password"]; 35: } 36: } 37: } 38: }

讀取頁面:

1: using System; 2: using System.Collections.Generic; 3: using System.Linq; 4: using System.Web; 5: using System.Web.UI; 6: using System.Web.UI.WebControls; 7: ? 8: using System.Configuration; 9: using KevinDiao.MySectionDemo01; 10: using KevinDiao.MySectionDemo02; 11: ? 12: namespace KevinDiao.AspNetDemo01 13: { 14: public partial class _Default : System.Web.UI.Page 15: { 16: protected void Page_Load(object sender, EventArgs e) 17: { 18: 19: ? 20: MySectionHandle mySectionHandle = (MySectionHandle)ConfigurationManager.GetSection("MySectionHandle02"); 21: Response.Write("username:"+mySectionHandle .Users .UserName+"<br/>"); 22: Response.Write("password:" + mySectionHandle.Users.Password + "<br/>"); 23: ? 24: } 25: } 26: }

web.config中的配置信息

1: <configSections> 2: <section name ="MySectionHandle02" type="KevinDiao.MySectionDemo02.MySectionHandle,KevinDiao.MySectionDemo02"/> 3: </configSections> 4: ? 5: <MySectionHandle02> 6: <users username="kevin" password="123"></users> 7: </MySectionHandle02>

獲取的的結(jié)果:

username:kevin?
password:123

今晚就先到這里了,下一篇我們在討論一下SectionGroup、SectionCollection等的應(yīng)用,最后在介紹個案例,加深大家的理解和在具體的項(xiàng)目中的應(yīng)用。

?

REFERENCE FROM : http://www.cnblogs.com/diaojia/archive/2011/03/29/1998280.html

總結(jié)

以上是生活随笔為你收集整理的ASP.NET杂谈-一切都从web.config说起(2)(ConfigSections详解-上 )的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。