在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
ConfigSections的结构 首先我们先回顾一下ConfigSections的结构和它子节点的说明,如下: 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属性和子节点说明 属性: 无。 子节点说明:
我们不难发现ConfigSectings主要包含SectiongGroup和Section两个子节点,下面就介绍一下这两个节点的属性说明: 1、sectionGroup属性说明
SectionGroup中还是可以在包含多个SectionGroup和Section。
2、section属性说明
其实在配置allowDefinition和allowExeDefinition属性的时候,他们其实是有选择值的。allowDefinition的值是在ConfigurationAllowDefinition 枚举中选择, 而allowExeDefinition的值是在ConfigurationAllowExeDefinition 枚举中选择。下面就介绍一下这两个枚举中各个值的的介绍: 1、ConfigurationAllowDefinition 枚举
注释: Machine.config的位置:%SystemRoot%\Microsoft.NET\Framework\versionNumber\CONFIG 中。 根 Web.config的位置:%SystemRoot%\Microsoft.NET\Framework\versionNumber\CONFIG 中。 2、ConfigurationAllowExeDefinition 枚举
举例说明 上面介绍了这么多,下面就用两个简单的Demo程序来介绍一下他们的具体应用吧: 1、Demo01介绍section的应用。 2、Demo02介绍包括子元素section的应用。 Demo01介绍一个配置用户信息的Section: 以下代码是Section的结构和属性的定义。 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的结构 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>
获取到的结果: UserName:kevindiao Password:123456 Demo02还是用用户信息来举例吧,以下是具有的代码: 以下是自定义Section的结构: 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>
获取的的结果: username:kevin 今晚就先到这里了,下一篇我们在讨论一下SectionGroup、SectionCollection等的应用,最后在介绍个案例,加深大家的理解和在具体的项目中的应用。
参考: |
请发表评论