Subscribe

RSS Feed (xml)

Storing and Reading Custom Settings from the web.config File

ASP.NET provides two ways to store information within the configuration file.The first


is to store simple name/value settings within an


<appSettings /> section,


The second is to create a custom configuration section that can store data


in any format.


In order to implement a custom configuration section handler, you must implement a


custom class that implements IConfigurationSectionHandler.The following is a partial


code sample for the new configuration section handler used for the ASP.NET


forums:


Imports System


Imports System.Collections


Imports System.Collections.Specialized


Imports System.Xml


Imports System.Configuration


Imports System.Web.Configuration


Namespace AspNetForums.Configuration


' *********************************************************************


' ForumsConfigurationHandler


'


' Class used by ASP.NET Configuration to load ASP.NET Forums configuration.


' ***********************************************************************/


Class ForumsConfigurationHandler


Implements IConfigurationSectionHandler


Public Overridable Overloads Function Create(ByVal parent As Object,


➥ ByVal configContext As Object, ByVal node As XmlNode) As Object


➥Implements IConfigurationSectionHandler.Create


Dim config As New ForumConfiguration(CType(parent,


ForumConfiguration))


config.LoadValuesFromConfigurationXml(node)


Return config


End Function


End Class


' *********************************************************************


' ForumConfiguration


'


' Class used to represent the configuration data for the ASP.NET Forums


' ***********************************************************************/


Public Class ForumConfiguration


Dim _pageSize As Integer = 25


' *****************************************************************


' ForumConfiguration


'


' Internal constructor for creating a new instance.


' ******************************************************************/


Public Sub New(ByVal parent As ForumConfiguration)


' Place holder for future work


End Sub


' *********************************************************************


' LoadValuesFromConfigurationXml


'


' Loads the forums configuration values.


' ******************************************************************/


Sub LoadValuesFromConfigurationXml(ByVal node As XmlNode)


Dim attributeCollection As XmlAttributeCollection = node.Attributes


' Get the attributes of the <forums> element


_pageSize =


Int32.Parse(attributeCollection("defaultPageSize").Value)


End Sub


' Properties


Public ReadOnly Property PageSize() As Integer


Get


Return _pageSize


End Get


End Property


End Class


End Namespace


This configuration section handler is compiled and then registered within an application.


It can then be used within web.config:


<configuration>


<configSections>


<sectionGroup name="system.web">


<section name="forums"


type="AspNetForums.Configuration.ForumsConfigurationHandler, AspNetForums" />


</sectionGroup>


</configSections>


<system.web>


<forums defaultPageSize = "25" />


</system.web>


</configuration>


Comments


This code example shows how you can create a custom configuration section. It's obviously


not a simple task. However, for controlling exactly how the configuration information


represents the data stored in the configuration file, it is the only option. If having a


custom configuration section seems to you like too much work just to store application


items, you're not alone.

No comments:

Archives

Variety in the Web World