Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Creating Custom Configuration Settings in ASP.NET  : Page 3

You don't have to limit yourself to the simple key/value settings available by default through the <AppSettings> section in the Web.config file. Instead, create custom configuration handlers to manage information however you like.


advertisement

Defining Custom Handlers for Custom Tags
Suppose you wanted to define a list of articles, each of which has a title, a URL, and zero or more authors. A tag structure such as the following would be much easier to maintain than the generic <add key="article title" value="articleURL"/>:

   <articlesVB>
      <article title="Article 1 Title" 
         url="http://www.somewhere.com/article1.aspx">
         <authors>
            <author>Russell Jones</author>
         </authors>
      </article>
      <article title="Article 2 Title" 
         url="http://www.somewhere.com/article2.aspx" />
         <authors>
            <author>Russell Jones</author>
            <author>Barry Jones</author>
         </authors>
      <!-- more links here -->
   </articlesVB>
Add the <articlesVB> tag and its contents (the preceding XML fragment) to your Web.config file, just before the ending </configuration> tag and save your changes.

Author Note: Don't run the project yet. If you run the project now, you'll get an error because there's no handler defined for the <articlesVB> section.


To read the <articlesVB> tag from a configuration file, you need to create a custom handler. Creating a custom handler is not difficult, but requires a separate project, because the handler implementation searches for an exe or dll file with the handler name. I'll walk you through the process in VB.NET, but I've included functionally identical code in C# as well in the downloadable code for this article (see the References section).

Create a new Class Library project and name it CustomItemHandler. Delete the default class that VS creates and add a new class named CustomTagHandler to the project. Custom handlers must implement the IConfigurationSectionHandler interface, which has only one method, called Create. The method takes three parameters: a parent object, an HttpConfigurationContext object, and a 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
When the ASP.NET framework reads the <articlesVB> node, it creates an instance of the CustomTagHandler class and calls its Create method. The XmlNode parameter contains the settings you want to read—in this example, the <articlesVB> custom tag and its contents. Of the three parameters, you would typically only use the XmlNode, but to be complete, the parent object contains the configuration settings from any corresponding parent configuration section. The configContext parameter is an instance of the HttpConfigurationContext class and is useful primarily for obtaining the virtual path to the Web.config file.

You're free to make the contents of custom configuration sections as simple or as complex as you like. I've elected to make this example more complex than a simple name/value pair so that you can see some of the possibilities inherent in using XML-formatted configuration files.

The Create method returns an Object. You can decide which type of object you want to return, but because you're implementing an interface method, you can't change the return type; therefore, code that calls your custom handler must cast the object to the correct type.

The CustomTagHandler class reads the list of child articles from the <articlesVB> tag and populates an ArrayList with a list of Article objects. Each Article object has three public read-only properties that hold the title, the URL, and the list of authors for each article. Note that because there may be an arbitrary number of authors, the Article object implements the list of authors as an ArrayList as well. It's important to realize that you do not have to treat the data in this manner. You could just as easily return the XML node itself and let the calling program deal with extracting the data, or you could return the author list as a collection of XML nodes, or—whatever you need. The point is that creating a custom handler lets you treat the data however you like. Here's the full code for the CustomTagHandler class and the Article class.



   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)
               Next
            End If
            articles.Add(New Article(aTitle, aURL, authors))
         Next
         Return articles
      End Function
   End Class
   
   Public Class Article
      Private m_title, m_url As String
      Private m_authors As ArrayList
      Public Sub New
       (ByVal aTitle As String, ByVal aURL As String, ByVal authors As ArrayList)
         m_title = aTitle
         m_url = aURL
         m_authors = authors
      End Sub
   
      Public ReadOnly Property Title() As String
         Get
            Return m_title
         End Get
      End Property
   
      Public ReadOnly Property URL() As String
         Get
            Return m_url
         End Get
      End Property
   
      Public ReadOnly Property Authors() As ArrayList
         Get
            Return m_authors
         End Get
      End Property
   
   End Class


Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date