Essential ASP.NET 读书笔记--Configuration
2004-07-10 13:55
429 查看
Asp.Net中使用Config文件是有层次关系的,就是常说的Web.Config文件;
对于放在不同虚拟目录下的Config文件的作用域是不一样的;就如下图所示:
object settings =
ConfigurationSettings.GetConfig("appSettings");
NameValueCollection nvc = settings as NameValueCollection;
if (nvc != null)
而静态索引器ConfigurationSettings.AppSettings["xxx"]就是对以上方法的一个方便的包装;
理论上说.Net的Config文件被分为两部分:configuration section handlers和configuration data
一个自定义Section Handler的例子:
首先,自己的配置Xml文件:
<configuration>
<acmeGroup>
<acme>
<font>Courier New</font>
<backgroundColor>Green</backgroundColor>
<underlineLinks>true</underlineLinks>
<horizontalWidth>600</horizontalWidth>
<verticalWidth>800</verticalWidth>
</acme>
</acmeGroup>
</configuration>
然后是对应的一个保存数据的映射类:
// File: AcmeSettings.cs
namespace EssentialAspDotNet.Config
然后,实现自己的读取相应格式配置的Handler,实现一个接口IConfigurationSectionHandler:
// File: AcmeConfigHandler.cs
namespace EssentialAspDotNet.Config
当然,如果不原意实现自己的Handler的话,也可以使用和AppSettings相同的策略来读取配置,但是必须使用
<add>元素来标记配置;实际上就是一个键值对,配置文件如下:
<configuration>
<configSections>
<section name="myGroup"
type="System.Configuration.NameValueFileSectionHandler, System, Version=1.0.3300.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"/>
</configSections>
<myGroup>
<add key="font" value="Courier New"/>
<add key="backgroundColor" value="Green"/>
<add key="underlineLinks" value="true"/>
<add key="horizontalWidth" value="600"/>
<add key="verticalWidth" value="800"/>
</myGroup>
</configuration>
相应使用的方法如下:
// File: TestAcmeSettings.aspx
protected void Page_Load(object src, EventArgs e)
[/code]
对于放在不同虚拟目录下的Config文件的作用域是不一样的;就如下图所示:
object settings =
ConfigurationSettings.GetConfig("appSettings");
NameValueCollection nvc = settings as NameValueCollection;
if (nvc != null)
而静态索引器ConfigurationSettings.AppSettings["xxx"]就是对以上方法的一个方便的包装;
理论上说.Net的Config文件被分为两部分:configuration section handlers和configuration data
一个自定义Section Handler的例子:
首先,自己的配置Xml文件:
<configuration>
<acmeGroup>
<acme>
<font>Courier New</font>
<backgroundColor>Green</backgroundColor>
<underlineLinks>true</underlineLinks>
<horizontalWidth>600</horizontalWidth>
<verticalWidth>800</verticalWidth>
</acme>
</acmeGroup>
</configuration>
然后是对应的一个保存数据的映射类:
// File: AcmeSettings.cs
namespace EssentialAspDotNet.Config
然后,实现自己的读取相应格式配置的Handler,实现一个接口IConfigurationSectionHandler:
// File: AcmeConfigHandler.cs
namespace EssentialAspDotNet.Config
当然,如果不原意实现自己的Handler的话,也可以使用和AppSettings相同的策略来读取配置,但是必须使用
<add>元素来标记配置;实际上就是一个键值对,配置文件如下:
<configuration>
<configSections>
<section name="myGroup"
type="System.Configuration.NameValueFileSectionHandler, System, Version=1.0.3300.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"/>
</configSections>
<myGroup>
<add key="font" value="Courier New"/>
<add key="backgroundColor" value="Green"/>
<add key="underlineLinks" value="true"/>
<add key="horizontalWidth" value="600"/>
<add key="verticalWidth" value="800"/>
</myGroup>
</configuration>
相应使用的方法如下:
// File: TestAcmeSettings.aspx
protected void Page_Load(object src, EventArgs e)
[/code]
相关文章推荐
- Essential ASP.NET 读书笔记--WebForm
- Essential ASP.NET 读书笔记--基础
- 解读ASP.NET 5 & MVC6系列教程(5):Configuration配置信息管理
- Essential Studio for ASP.NET MVC发布2017 v2,增加日期范围选择器功能
- 【asp.net】Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.EnterpriseLibraryFactory的类型初始值设定项引发异常
- 【读书笔记】ASP.NET State Management
- WCF REST Configuration for ASP.NET AJAX and plain REST Services
- [转载]Encrypting Configuration Information in ASP.NET 2.0 Applications
- 《Essential .Net》读书笔记 - Chapter 1
- 【读书笔记】Introducing ASP.NET
- Essential Chart for ASP.NET MVC商业图表控件相关介绍及下载
- Accessing the ASP.NET Web Configuration Tool in Visual Studio 2013
- Essential .NET 读书笔记 [第一部分]
- 原创:ASP.Net状态管理读书笔记--思维导图
- Asp.Net Core 中无法使用 ConfigurationManager.AppSettings
- [Professional ASP.NET Design Pattern 读书笔记系列] 01 成功应用的模式
- 《Asp.Net 2.0 揭秘》读书笔记(十一)
- ASP.NET 2.0 读书笔记 圣殿 之 玩转GridView(1)
- 网站的ASP.NET 配置页面无法打开(ASP.NET Configuration)(找不到存储过程 'dbo.aspnet_CheckSchemaVersion')