Skip to Content
ASP.NET Cookbook
book

ASP.NET Cookbook

by Michael A Kittel, Geoffrey T. LeBlond
August 2004
Intermediate to advanced
848 pages
23h
English
O'Reilly Media, Inc.
Content preview from ASP.NET Cookbook

9.2. Adding Custom Application Settings in web.config

Problem

You have custom configuration information for your application that you would like to store in its web.config file.

Solution

Modify the web.config file for your application by adding an <appSettings> section to contain the custom configuration settings.

  1. Locate the web.config file in the root directory of your application (or create one if it does not already exist).

  2. Add an <appSettings> element.

  3. Add <add> child elements along with key/value pairs to the <appSettings> element as required.

  4. In the code-behind class for your ASP.NET page, use the .NET language of your choice to access the <appSettings> key/value collection through the ConfigurationSettings object.

Example 9-1 through Example 9-4 show the sample code we’ve written to implement this solution. Example 9-1 shows our web.config file with some key/value pairs. Example 9-2 shows the .aspx file for a web form that displays the configuration settings. Example 9-3 (VB) and Example 9-4 (C#) show the code-behind class that accesses the configuration settings using the ConfigurationSettings object.

Discussion

ASP.NET lets you add and then access configuration information specific to your application to the web.config file by means of a special <appSettings> element. You can add application configuration information by adding an <add> child element for each parameter, setting the key attribute to the name of the configuration parameter, and then setting the value attribute to ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

ASP.NET 2.0 Cookbook, 2nd Edition

ASP.NET 2.0 Cookbook, 2nd Edition

Michael A Kittel, Geoffrey T. LeBlond
Building Web APIs with ASP.NET 4.5

Building Web APIs with ASP.NET 4.5

John Adams, Ryan Butler

Publisher Resources

ISBN: 0596003781Catalog PageErrata