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属性和子节点说明    


属性:

无。

子节点说明:

节点名称

功能描述

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的值是在ConfigurationAllowDefinition 枚举中选择,

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

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:      /// <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 
password:123

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

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

ASP.NET杂谈-一切都从web.config说起(2)(ConfigSections详解-上 )的更多相关文章

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

    还是接着上一篇说起,在上两篇中主要和大家探讨了ConfigSection的几种常用形式,并举例几个例子说明了一下.其实它们主要都是继承System.Configuration.Configuratio ...

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

    我们就接着上一篇继续说,上一篇中介绍了ConfigSection的结构和两个简单的DEMO,本篇就说一下SectionGroup.ConfigurationElementCollection和key/ ...

  3. 10 Things ASP.NET Developers Should Know About Web.config Inheritance and Overrides(转)

    10 Things ASP.NET Developers Should Know About Web.config Inheritance and Overrides Wednesday, Janua ...

  4. WCF项目问题2-无法激活服务,因为它需要 ASP.NET 兼容性。没有未此应用程序启用 ASP.NET 兼容性。请在 web.config 中启用 ASP.NET 兼容性,或将 AspNetCompatibilityRequirementsAttribute.AspNetCompatibilityRequirementsMode 属性设置为 Required 以外的值。

    无法激活服务,因为它需要 ASP.NET 兼容性.没有未此应用程序启用 ASP.NET 兼容性.请在 web.config 中启用 ASP.NET 兼容性,或将 AspNetCompatibility ...

  5. SAE上传web应用(包括使用数据库)教程详解及问题解惑

    转自:http://blog.csdn.net/baiyuliang2013/article/details/24725995 SAE上传web应用(包括使用数据库)教程详解及问题解惑: 最近由于工作 ...

  6. ASP.NET MVC 5 学习教程:Edit方法和Edit视图详解

    原文 ASP.NET MVC 5 学习教程:Edit方法和Edit视图详解 起飞网 ASP.NET MVC 5 学习教程目录: 添加控制器 添加视图 修改视图和布局页 控制器传递数据给视图 添加模型 ...

  7. 【译】ASP.NET MVC 5 教程 - 7:Edit方法和Edit视图详解

    原文:[译]ASP.NET MVC 5 教程 - 7:Edit方法和Edit视图详解 在本节中,我们继续研究生成的Edit方法和视图.但在研究之前,我们先将 release date 弄得好看一点.打 ...

  8. Java web 入门知识 及HTTP协议详解

     Java  web  入门知识 及HTTP协议详解 WEB入门 WEB,在英语中web即表示网页的意思,它用于表示Internet主机上供外界访问的资源. Internet上供外界访问的Web资 ...

  9. 巨人大哥谈Web应用中的Session(session详解)

    巨人大哥谈Web应用中的Session(session详解) 虽然session机制在web应用程序中被采用已经很长时间了,但是仍然有很多人不清楚session机制的本质,以至不能正确的应用这一技术. ...

随机推荐

  1. [No000028]Python的使用之禅及程序员应该明白的一些道理

    最近疯狂的学习ruby和python着两门语言,看到书本上一些比较赞同的段子,摘抄并翻译如下: Python的使用之禅 Beautiful is better than ugly. 美丽好过丑陋Exp ...

  2. MVC code first数据迁移 转

    coptto:http://www.cnblogs.com/miro/p/4164076.html 本篇是相对独立的一篇,主要讲解不丢失数据进行数据库结构升级. 前面我们讲解EF功能时(见第三篇文章) ...

  3. 開博客了, 因為搞Delphi 開發的關於Delphi學習

    開博客了, 因為搞Delphi 開發的關於Delphi學習,之前都是用本地TXT文件保存,發現在本地電腦保存非常不方面,而且只能在一台電腦上保存,不容易查看和修改內容.便於以後的記錄只用,以及經驗交流 ...

  4. C# winform生成天气预报(转)

    原文地址 http://www.cnblogs.com/ChowYy/p/3382216.html?utm_source=tuicool&utm_medium=referral 项目需要,然后 ...

  5. android values目录的读取优先级

    android项目新建时会有一个values目录(高版本会增加values-v11,values-v14目录),该目录用于存放显示相的配置数据的定义文件,如strings.xml, style.xml ...

  6. 清北学堂2017NOIP冬令营入学测试 P4744 A’s problem(a)

    清北学堂2017NOIP冬令营入学测试 P4744 A's problem(a) 时间: 1000ms / 空间: 655360KiB / Java类名: Main 背景 冬令营入学测试题,每三天结算 ...

  7. eclipse/intellij idea 远程调试hadoop 2.6.0

    很多hadoop初学者估计都我一样,由于没有足够的机器资源,只能在虚拟机里弄一个linux安装hadoop的伪分布,然后在host机上win7里使用eclipse或Intellj idea来写代码测试 ...

  8. Android -- Apk安装简诉

    安装涉及到如下几个目录 system/app  ​ 系统自带的应用程序,无法删除 data/app   用户程序安装的目录,有删除权限. 安装时把apk文件复制到此目录 ​ data/data  存放 ...

  9. LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree

    LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...

  10. ubuntu-12.10-server中打开终端的方式

    ubuntu-12.10-server系统在图形界面的任务栏上找不到终端的踪影,可以使用以下两种方式调出 1.在图形界面中点击Dash Home 点击后搜索terminal即可 2.可以通过快捷键CT ...