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:

/book/samples/web.config

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

These settings are now read by an aspx page:

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

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

    string GetAdminEmail() {
      return ConfigurationSettings.AppSettings["AdminEmail"];
    }
  </script>

  <head>
    <title>Application Settings</title>
  </head>

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

Try it

   http://dotnet.jku.at/book/samples/6/AppSettings.aspx