一、介绍
ASP.NET Web应用程序用一种内置的方法访问简单的“键/值”配置数据。在Web.config文件中,你可以创建
该节包含了用两个
Dim aKey As String
Response.Write(" For Each aKey In ConfigurationSettings.AppSettings.Keys Response.Output.WriteLine(aKey & "=" & _ ConfigurationSettings.AppSettings.Item(aKey)) Next 编译运行customItems.aspx Web窗体,就能看到 在解析配置文件时,ASP.NET引擎通过读取 保存Web.config文件并运行项目,将会得到一个“无法识别的配置节‘customItems’”的错误,这个错误的发生是由于没有声明 事实上每一个Web应用程序都有两个配置文件:保存在系统文件夹下的根machine.config文件和在你应用程序根目录下的Web.config文件。你可以在操作系统文件夹下的Microsoft.NETFramework 我之所以引出machine.config文件,是因为有两种方法添加自定义标记:可以用任一种缺省的系统配置节处理程序来解析自定义标记内容,也可以创建你自己的配置节处理程序。 二、使用系统配置节处理程序解析自定义标记 1.在 type="System.Configuration.NameValueSectionHandler,System, Version=1.0.3300.0,Culture=neutral,PublicKeyToken=b77a5c561934e089" /> 作者提醒:Version和PublicKeyToken的值可能和你的.NET框架版本不同,要在系统中找出正确的值只需从任一个已存在的 2.将新建的 3.保存Web.config文件,将如下高亮部分代码增加到customItems.aspx Web窗体中的Page_Load事件中: Dim aKey As String Response.Write(" For Each aKey In ConfigurationSettings.AppSettings.Keys Response.Output.WriteLine(aKey & "=" & _ ConfigurationSettings.AppSettings.Item(aKey)) Next Response.Write(" For Each aKey In CType(ConfigurationSettings.GetConfig _ ("customSystemItems"), _ System.Collections.Specialized.NameValueCollection).Keys Response.Output.WriteLine(aKey & "=" & _ ConfigurationSettings.AppSettings.Item(aKey)) Next 4.现在再次编译执行该Web窗体。这次,可以看到CustomSystemItem头信息尾随了一行“SomeKey=SomeValue”,它对应着在 通过修改machine.config文件,可以将定义好的自定义标记应用到在服务器上运行的任意Web应用程序上。但很可能你并不总是希望标记处理程序应用于所有应用程序,如果是这样,可以在Web.config文件中增加 type="System.Configuration.NameValueSectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> 5.为了能够看到变化,在web.config文件中的 保存web.config文件并再次运行customItems.aspx Web窗体,可以看到有两个值而不再只是一个。无需重新编译应用程序即可完成测试,ASP.NET能够立即应用新的配置。 使用这种方式可以定义任意数量的自定义标记。然而,使用普通的 三、定义解析自定义标记的自定义配置节处理程序 假定需要定义一系列文章,每一篇都有一个标题,一个URL,没有或有多个作者,那么如下所示的标记结构就会比泛泛的 url="http://www.somewhere.com/article1.aspx"> url="http://www.somewhere.com/article2.aspx" /> 增加 作者提醒:先不要运行这个项目,否则会因为还没有为 为了能够从配置文件中识别 新建一个类库项目并命名为CustomItemHandler,删除VS默认生成的类而在项目中添加一个名为CustomTagHandler的新类。自定义配置节处理程序必须实现IconfigurationSectionHandler接口,这个接口只有一个名为Create的方法,该方法接受三个参数:一个名为parent的object变量,一个HttpConfigurationContext对象变量,一个名为section的XmlNode变量。 Imports System Imports System.Collections Imports System.Xml Imports System.Configuration Public Class CustomTagHandler Implements IConfigurationSectionHandler Public Function Create(ByVal parent As Object, _ ByVal configContext As Object, _ ByVal section As System.Xml.XmlNode) As Object _ Implements System.Configuration. _ IConfigurationSectionHandler.Create ' Implementation here End Class 当ASP.NET框架读到 你可以按照自己的意愿设置自定义配置节的内容,简单或复杂均可。我则选择一种比简单“键/值”对要复杂一些的例子,以展示使用XML格式配置文件的可行性。 Create方法返回了一个Object对象,你可以决定你希望返回的对象类型,但因为这是在实现接口方法,所以不能改变方法的返回类型。也正因为此,调用自定义配置节处理程序的代码必需将返回对象转换成正确类型。 CustomTagHandler类在读取 Imports System Imports System.Collections Imports System.Xml Imports System.Configuration Public Class CustomTagHandler Implements IConfigurationSectionHandler Public Function Create(ByVal parent As Object, _ ByVal configContext As Object, _ ByVal section As System.Xml.XmlNode) As Object _ Implements System.Configuration. _ IConfigurationSectionHandler.Create Dim NArticle, NAuthor As XmlNode Dim articleNodes, authorNodes As XmlNodeList Dim authors As ArrayList Dim aTitle As String Dim aURL As String Dim articles As New ArrayList() articleNodes = section.SelectNodes("article") For Each NArticle In articleNodes aTitle = NArticle.Attributes.GetNamedItem("title").Value aURL = NArticle.Attributes.GetNamedItem("url").Value authors = New ArrayList() authorNodes = NArticle.SelectNodes("authors//author") If Not authorNodes Is Nothing Then For Each NAuthor In authorNodes authors.Add(NAuthor.InnerText)AppSettings
") 
AppSettings
") CustomSystemItems
")
