Browse DevX
Sign up for e-mail newsletters from DevX


Creating Custom Configuration Settings in ASP.NET

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.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

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 <appSettings> section that lets you store simple name/value pairs. For example, create a new ASP.NET project and add the following <appSettings> tag as a child of the <configuration> element in your Web.config file:

<configuration> <add key="SomeKey" value="SomeValue"/> <add key="AnotherKey" value="AnotherValue"/> </configuration>

The section contains two <add> tags that define the key/value pairs. You can retrieve the values via the built-in ConfigurationSettings property of the Page object. To begin, create a new Web Form in your project, name it customItems.aspx, and add this code to the Page_Load event:

Dim aKey As String Response.Write("<h3>AppSettings</h3>") For Each aKey In ConfigurationSettings.AppSettings.Keys Response.Output.WriteLine(aKey & "=" & _ ConfigurationSettings.AppSettings.Item(aKey)) Next

Figure 1. ConfigSections: The relationship between configuration file sections, tags, and handlers.
Compile and run the customItems.aspx Web Form. You'll see the <AppSettings> tag values. The loop retrieves all the <add> tags from the <appSettings> section, and displays the key and value attribute values (see the sidebar "TIP: Using the Response.Output.WriteLine Method" for a discussion of how the code writes individual HTML lines). This simple key/value mechanism is perfect for many common needs, such as storing database connection strings at application scope; but it's not robust enough for more complex data. Fortunately, Microsoft also built in a mechanism for creating custom configuration data. Rather than reading a hard-coded list of tags recognized only via code within a specific application, the ASP.NET framework reads one or more <configSections> sections, which define the tag names the framework should expect to find in the remainder of the file and also define a class type and location for handling that particular type of content (see Figure 1).

As the ASP.NET engine parses the configuration file, it builds a list of the possible tags by reading the <configSections> element's <section> tags, each of which contains a name and a type, that define the names of the tags expected in the remainder of the document and a handler for that particular tag. A little experimentation shows you how this works. In the Web.config file for your project, add a new tag just before the ending </configuration> tag in the file.

<configuration> <!—the existing web.config settings here --> <customItems> </customItems> </configuration>

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 <customItems> tag. But if you look at the entire Web.config file, there are no handlers defined for any of the tags, which leads to the question: Where are the handlers defined? (If you're performing the steps as you read this article, remove the <customItems> tag from your Web.config file before you continue.)

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.NET\Framework\<version>\CONFIG folder, where <version> corresponds to the version of the framework installed and active on your server. The configuration settings in the machine.config file apply to all applications on your server unless overridden by more localized settings. If you look at the machine.config file, you'll see a <configSections> tag containing a set of <section> tags—and those tags define the handlers for the default tags you see in the web.config file. To make the process even simpler to understand, you can further group the <section> tags inside <sectionGroup> tags, each of which defines a set of handlers for related section tags.

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.

Comment and Contribute






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



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