Home   Cover Cover Cover Cover

Application Configuration (web.config and machine.config)

From Section 6.9.2 of the book

This example shows how to read the settings in the files web.config and machine.config.

Assume that the file web.config, which is stored in the application's virtual directory, contains settings for the definition of a help page and the web master's e-mail adderess:


Error: cannot show file /book/samples/web.config

These settings are now read by an aspx page:

<%@ Page Language="C#"%>
<%@ Import Namespace="System.Configuration" %>

  <script Language="C#" Runat="server">
    string GetHelpHomepage() {
      return ConfigurationSettings.AppSettings["DefaultHelpHomepage"];

    string GetAdminEmail() {
      return ConfigurationSettings.AppSettings["AdminEmail"];

    <title>Application Settings</title>

    For help refer to:
    <a href="<% Response.Write(GetHelpHomepage()); %>">
    <% Response.Write(GetHelpHomepage()); %></a>
    Web master:
    <a href="mailto:<% Response.Write(GetAdminEmail()); %>">
    <% Response.Write(GetAdminEmail()); %></a>

Try it