您的位置:首页 > 编程语言 > ASP

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

2011-03-29 00:19 351 查看
ConfigSections的结构

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

<configSections>

[code]<sectionGroupname="system.web.extensions"type="System.Web.Configuration.SystemWebExtensionsSectionGroup,System.Web.Extensions,Version=3.5.0.0,Culture=neutral,PublicKeyToken=31BF3856AD364E35">
<sectionGroupname="scripting"type="System.Web.Configuration.ScriptingSectionGroup,System.Web.Extensions,Version=3.5.0.0,Culture=neutral,PublicKeyToken=31BF3856AD364E35">

<sectionname="scriptResourceHandler"type="System.Web.Configuration.ScriptingScriptResourceHandlerSection,System.Web.Extensions,Version=3.5.0.0,Culture=neutral,PublicKeyToken=31BF3856AD364E35"

requirePermission="false"allowDefinition="MachineToApplication"/>

<sectionGroupname="webServices"type="System.Web.Configuration.ScriptingWebServicesSectionGroup,System.Web.Extensions,Version=3.5.0.0,Culture=neutral,PublicKeyToken=31BF3856AD364E35">

<sectionname="jsonSerialization"type="System.Web.Configuration.ScriptingJsonSerializationSection,System.Web.Extensions,Version=3.5.0.0,Culture=neutral,PublicKeyToken=31BF3856AD364E35"

requirePermission="false"allowDefinition="Everywhere"/>

<sectionname="profileService"type="System.Web.Configuration.ScriptingProfileServiceSection,System.Web.Extensions,Version=3.5.0.0,Culture=neutral,PublicKeyToken=31BF3856AD364E35"

requirePermission="false"allowDefinition="MachineToApplication"/>

<sectionname="authenticationService"type="System.Web.Configuration.ScriptingAuthenticationServiceSection,System.Web.Extensions,Version=3.5.0.0,Culture=neutral,PublicKeyToken=31BF3856AD364E35"

requirePermission="false"allowDefinition="MachineToApplication"/>

<sectionname="roleService"type="System.Web.Configuration.ScriptingRoleServiceSection,System.Web.Extensions,Version=3.5.0.0,Culture=neutral,PublicKeyToken=31BF3856AD364E35"

requirePermission="false"allowDefinition="MachineToApplication"/>

</sectionGroup>

</sectionGroup>

</sectionGroup>

</configSections>

[/code]

ConfigSectins属性和子节点说明

属性:

无。

子节点说明:

节点名称

功能描述

sectionGroup

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

section

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

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

1、sectionGroup属性说明

属性名称

功能描述

name

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

type

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

type="Fullyqualifiedclassname,assemblyfilename,version,culture,publickeytoken",定义必须匹配程序集引用。

程序集文件必须与定义它的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

指定可以在哪个配置文件中使用该节,仅适用于.NETFramework客户端应用程序,可选的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的结构和属性的定义。

usingSystem;

[code]usingSystem.Collections.Generic;
usingSystem.Linq;

usingSystem.Text;


usingSystem.Configuration;


namespaceKevinDiao.MySectionDemo01

{

///<summary>

///自定义Section的结构

///</summary>

publicclassMySection:ConfigurationSection

{

///<summary>

///用户名称

///</summary>

[ConfigurationProperty("username",IsRequired=true)]

publicstringUserName

{

get

{

return(string)this["username"];

}

set

{

this["username"]=value;

}

}

///<summary>

///用户密码

///</summary>

[ConfigurationProperty("password",IsRequired=true)]

publicstringPassword

{

get

{

return(string)this["password"];

}

set

{

this["password"]=value;

}

}

}

}

[/code]

下面介绍时简单的获取程序:

usingSystem;

[code]usingSystem.Collections.Generic;
usingSystem.Linq;

usingSystem.Web;

usingSystem.Web.UI;

usingSystem.Web.UI.WebControls;


usingSystem.Configuration;

usingKevinDiao.MySectionDemo01;


namespaceKevinDiao.AspNetDemo01

{

publicpartialclass_Default:System.Web.UI.Page

{

protectedvoidPage_Load(objectsender,EventArgse)

{

MySectionmySection=(MySection)ConfigurationManager.GetSection("MySectionHandle01");

Response.Write("UserName:"+mySection.UserName+"<br/>");

Response.Write("Password:"+mySection.Password);

}

}

}

[/code]

web.config中的配置信息

<configSections>

[code]<sectionname="MySectionHandle01"type="KevinDiao.MySectionDemo01.MySection,KevinDiao.MySectionDemo01"/>
</configSections>

<MySectionHandle01username="kevindiao"password="123456"></MySectionHandle01>

[/code]

获取到的结果:

UserName:kevindiao

[code]Password:123456
[/code]

Demo02还是用用户信息来举例吧,以下是具有的代码:

以下是自定义Section的结构:

usingSystem;

[code]usingSystem.Collections.Generic;
usingSystem.Linq;

usingSystem.Text;


usingSystem.Configuration;


namespaceKevinDiao.MySectionDemo02

{

///<summary>

///自定义Section

///</summary>

publicclassMySectionHandle:ConfigurationSection

{

[ConfigurationProperty("users",IsRequired=true)]

publicMySectionElementUsers

{

get

{

return(MySectionElement)this["users"];

}

}

}

}

[/code]

自定义Element:

usingSystem;

[code]usingSystem.Collections.Generic;
usingSystem.Linq;

usingSystem.Text;


usingSystem.Configuration;


namespaceKevinDiao.MySectionDemo02

{

///<summary>

///自定义Element

///</summary>

publicclassMySectionElement:ConfigurationElement

{

///<summary>

///用户名

///</summary>

[ConfigurationProperty("username",IsRequired=true)]

publicstringUserName

{

get

{

return(string)this["username"];

}

}

///<summary>

///密码

///</summary>

[ConfigurationProperty("password",IsRequired=true)]

publicstringPassword

{

get

{

return(string)this["password"];

}

}

}

}

[/code]

读取页面:

usingSystem;

[code]usingSystem.Collections.Generic;
usingSystem.Linq;

usingSystem.Web;

usingSystem.Web.UI;

usingSystem.Web.UI.WebControls;


usingSystem.Configuration;

usingKevinDiao.MySectionDemo01;

usingKevinDiao.MySectionDemo02;


namespaceKevinDiao.AspNetDemo01

{

publicpartialclass_Default:System.Web.UI.Page

{

protectedvoidPage_Load(objectsender,EventArgse)

{



MySectionHandlemySectionHandle=(MySectionHandle)ConfigurationManager.GetSection("MySectionHandle02");

Response.Write("username:"+mySectionHandle.Users.UserName+"<br/>");

Response.Write("password:"+mySectionHandle.Users.Password+"<br/>");


}

}

}

[/code]

web.config中的配置信息

<configSections>

[code]<sectionname="MySectionHandle02"type="KevinDiao.MySectionDemo02.MySectionHandle,KevinDiao.MySectionDemo02"/>
</configSections>


<MySectionHandle02>

<usersusername="kevin"password="123"></users>

</MySectionHandle02>

[/code]

获取的的结果:

username:kevin
password:123

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

参考:

MSDN
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: