博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ASP.NET杂谈-一切都从web.config说起(2)(ConfigSections详解-上 )
阅读量:6933 次
发布时间:2019-06-27

本文共 7665 字,大约阅读时间需要 25 分钟。

ConfigSections的结构


首先我们先回顾一下ConfigSections的结构和它子节点的说明,如下:

1:  
2:        
3:          
4:            
5:                     requirePermission="false" allowDefinition="MachineToApplication"/>
6:            
7:              
8:                       requirePermission="false" allowDefinition="Everywhere" />
9:              
10:                       requirePermission="false" allowDefinition="MachineToApplication" />
11:              
12:                       requirePermission="false" allowDefinition="MachineToApplication" />
13:              
14:                       requirePermission="false" allowDefinition="MachineToApplication" />
15:            
16:          
17:        
18:      

 

ConfigSectins属性和子节点说明    


属性:

    无。

子节点说明:

节点名称

功能描述

sectionGroup

定义配置节处理程序与配置之间的关联。

section

定义配置节处理程序与配置元素之间的关联。

我们不难发现ConfigSectings主要包含SectiongGroup和Section两个子节点,下面就介绍一下这两个节点的属性说明:

1、sectionGroup属性说明

 


属性名称

功能描述

name

指定与下面 type 属性指定的配置节处理程序关联的配置节或元素的名称。

type

指定用来执行如下操作的配置节处理程序类的名称:处理在 name 属性中指定的节或元素中的配置设置。使用以下格式:

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

程序集文件必须与定义它的 Web.config 文件位于同一个应用程序目录中。

SectionGroup中还是可以在包含多个SectionGroup和Section。

 

 

2、section属性说明


属性名称

功能描述

name

指定与 type 属性中指定的配置节处理程序关联的配置节或元素的名称。

type

指定用来执行如下操作的配置节处理程序类的名称:处理在 name 属性中指定的节或元素中的配置设置,格式和上面sectionGroup属性中的type格式相同。

requirePermission

指定是否得到相关的配置部分要求存取权限信息。可选的 Boolean 属性。

restartOnExternalChanges

指定在该节的配置数据发生更改时是否应当重新启动应用程序,不适用于 ASP.NET 应用程序,可选的 Boolean 属性。

allowLocation

指定是否可以在 location 元素内使用该节,仅适用于 ASP.NET 应用程序,默认值为 True。

allowExeDefinition

指定可以在哪个配置文件中使用该节,仅适用于 .NET Framework 客户端应用程序,可选的 Boolean 属性。

allowDefinition

指定可以在哪个配置文件中使用该节,仅适用于 ASP.NET 应用程序,可选的 Boolean 属性。

其实在配置allowDefinition和allowExeDefinition属性的时候,他们其实是有选择值的。allowDefinition的值是在 枚举中选择,

而allowExeDefinition的值是在 枚举中选择。下面就介绍一下这两个枚举中各个值的的介绍:

1、ConfigurationAllowDefinition 枚举


 

描述

Everywhere

允许在任何配置文件或目录中配置该节,如下所示:

  • Machine.config。

  • 根 Web.config。

  • 应用程序的 Web.config。

  • 虚拟目录。

  • 应用程序中的物理子目录。

如果未使用 allowDefinition 属性,则假设为 Everywhere。这是默认设置。

MachineToApplication

允许在下列文件之一中配置该节:

  • Machine.config。

  • 根 Web.config。

  • 应用程序的 Web.config。

这不包括位于应用程序中的虚拟目录或物理子目录下的 Web.config 文件。

MachineToWebRoot

允许在下列文件之一中配置该节:

  • Machine.config。

  • 根 Web.config。

MachineOnly

只允许在 Machine.config 文件中配置该节。

注释:

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

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

2、ConfigurationAllowExeDefinition 枚举


 

描述

MachineToApplication

可在 Machine.config 文件或客户端应用程序目录中的 Exe.config 文件中定义 ConfigurationSection。

MachineToRoamingUser

可在 Machine.config 文件、客户端应用程序目录中的 Exe.config 文件、漫游用户目录中的 User.config 文件或本地用户目录中的 User.config 文件中定义 ConfigurationSection。

MachineToLocalUser

可在 Machine.config 文件、客户端应用程序目录中的 Exe.config 文件或漫游用户目录中的 User.config 文件中定义 ConfigurationSection

MachineOnly

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

举例说明


 

上面介绍了这么多,下面就用两个简单的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:      /// 
11:      /// 自定义Section的结构
12:      /// 
13:      public class MySection:ConfigurationSection
14:      {
15:          /// 
16:          /// 用户名称
17:          /// 
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:          /// 
31:          /// 用户密码
32:          /// 
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+"
");
19:              Response.Write("Password:"+mySection .Password);
20:          }
21:      }
22:  }

web.config中的配置信息

1:  
2:    
3:   
4:   

 

获取到的结果:

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:      /// 
11:      /// 自定义Section
12:      /// 
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:      /// 
11:      /// 自定义Element
12:      /// 
13:      public class MySectionElement : ConfigurationElement
14:      {
15:          /// 
16:          /// 用户名
17:          /// 
18:          [ConfigurationProperty("username", IsRequired = true)]
19:          public string UserName
20:          {
21:              get
22:              {
23:                  return (string)this["username"];
24:              }
25:          }
26:          /// 
27:          /// 密码
28:          /// 
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+"
");
22:              Response.Write("password:" + mySectionHandle.Users.Password + "
");
23:   
24:          }
25:      }
26:  }

web.config中的配置信息

1:  
2:      
3:     
4:   
5:    
6:      
7:    

获取的的结果:

username:kevin 

password:123

今晚就先到这里了,下一篇我们在讨论一下SectionGroup、SectionCollection等的应用,最后在介绍个案例,加深大家的理解和在具体的项目中的应用。

 

REFERENCE FROM : 

转载于:https://www.cnblogs.com/zhangchenliang/p/4155337.html

你可能感兴趣的文章
FlashBuilder(FB/eclipse) 打开多个无效
查看>>
广播的接收与处理
查看>>
理解Kubernetes(2): 应用的各种访问方式
查看>>
由浅入深CIL系列【目录索引】+ PostSharp AOP编程【目录索引】
查看>>
js禁止用户右键等操作
查看>>
oracle表空间压缩
查看>>
Apache Spark Jobs 性能调优
查看>>
C# HashTable的用法总结
查看>>
如何在本机搭建SVN服务器【转】
查看>>
Oracle开发常用函数与存储过程
查看>>
修改PHP上传文件大小限制的方法
查看>>
OLAP与OLTP介绍
查看>>
Mac 安装md5sum等
查看>>
memcached client --ref
查看>>
MyBatis魔法堂:ResultMap详解
查看>>
《基于Windows 7特性的程序开发系列》视频分享
查看>>
SilverLight.3-Validation:二、银光验证。TheLabel、TheDescriptionViewer和TheValidationSummary...
查看>>
二叉树的非递归遍历(递归和非递归)
查看>>
第 13 章 编码风格
查看>>
WPF 浏览PDF 文件
查看>>