SP.NET Web applications have a built-in way to access simple name/value configuration data. In the Web.config file, you can create an
The section contains two
Dim aKey As String Response.Write(“
“) For Each aKey In ConfigurationSettings.AppSettings.Keys Response.Output.WriteLine(aKey & “=” & _ ConfigurationSettings.AppSettings.Item(aKey)) Next
Compile and run the customItems.aspx Web Form. You’ll see the
As the ASP.NET engine parses the configuration file, it builds a list of the possible tags by reading the
Save the Web.config file and run the project. You’ll get an “Unrecognized configuration in section ‘customItems’” error. The error occurs because there’s no section handler defined for the
It turns out that there are two configuration files for every Web application: a root machine.config file, stored in a system folder, and the Web.config file in your application’s root folder. You can find the machine.config file in the $System$Microsoft.NETFramework
The reason I brought up the machine.config file is because there are two ways to add custom tags. You can use one of the default System handler types to parse the content of your custom tags, or you can create your own handler.
|Author Note: The Version and PublicKeyToken values may differ on your version of the .NET framework. To find the appropriate values for your system, copy them from an existing element.|
2. Test the new
3. Save your changes to the web.config file. In your customItems.aspx Web Form, add the highlighted code to the Page_Load event:
4. Now compile and run the Web Form again. This time, you’ll see the CustomSystemItem header followed by the line “SomeKey=SomeValue“, which corresponds to the single
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
By modifying the machine.config file, you can use the defined custom tag in any Web application running on your server?but you may not always want the handler to apply to all applications. If you don’t, you can add a
5. So that you can see a change, append one more
Save the changes to your web.config file and run the customItems.aspx Web Form again. You will see two values now rather than one. You don’t need to recompile your application to perform the test. ASP.NET applies configuration changes immediately.
You can define any number of custom tags in this manner; however, using the generic
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
Russell Jones Russell Jones Barry Jones
|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 |
To read the
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.
When the ASP.NET framework reads the
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
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
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
Using the CustomItemHandler
Now you’re ready to test the CustomItemHandler class. First, make sure your code compiles without errors. To test the class, you need to add the
Next, make one more modification to your Web.config file. Within the
|WARNING: The Web.config file is XML and is therefore case sensitive! Make sure the attribute values match the case for both the tag and the assembly and class names.|
Add the following code to your customItems.aspx Web Form to retrieve and display the articles as links:
Finally, compile and run the customItems.aspx Web Form. You’ll see the header “ArticlesVB” followed by the list of article links defined by the
Dim articles As ArrayList Dim anArticleVB As CustomItemHandler.Article Dim o as Object Dim s As String Response.Write(“
“) articles = CType(System.Configuration. _ ConfigurationSettings.GetConfig _ (“articlesVB”), ArrayList) If Not articles Is Nothing Then For Each o In articles anArticleVB = CType(o, CustomItemHandler.Article) Response.Output.WriteLine _ (“” & anArticleVB.Title & _ ““) If Not anArticleVB.Authors Is Nothing Then s = “by ” For Each obj In anArticleVB.Authors s += CType(obj, String) & “, ” Next Response.Output.WriteLine _ (s.Substring(0, s.Length – 2)) End If Next End If
You can follow the steps in this article to create custom handlers for any type of information that you can store in a configuration file. You’ve seen how to use the built-in