If
your site is running from within a virtual directory (through IIS), you
can use the features view in IIS to edit configuration information. To
do so, your site must be managed by IIS.
Although you can
configure ASP.NET applications from IIS only on the computer that hosts
the site, the level of configuration you control is much more extensive
than it is when using the Web Site Administration Tool from within
Visual Studio. Your configuration changes also have an immediate
operational effect on your Web application.
Following is an exercise to help you become familiar with the ASP.NET Configuration tab in IIS.
Using IIS to configure ASP.NET
Before
creating this new Web site, be sure to execute Visual Studio as an
administrator (that is, right-click Visual Studio on the Start menu and
select Run As Administrator. This is necessary when creating (or editing) Web sites hosted directly by IIS. Create a new Web site called ConfigORamaIIS.
To host the
Web site directly within IIS, click the Browse button to open the
dialog box that lets you choose the location. Choose your own computer (localhost). Visual Studio will create a virtual directory for you and point itself to the virtual directory:
Open
the IIS management console. To do so, open Control Panel and then
Administrative Tools. If you are using Windows Vista or Windows 7, you
can access Administrative Tools through the System And Security
settings option. There, you should be able to open the Internet
Information Services (IIS) Manager. Look for the ConfigORamaIIS site in
the Connections pane. Click the ConfigORamaIIS virtual directory, and you'll see the ASP.NET-related settings appear in the Features View pane:
Double-click
one or two of the features to view their configuration screens. For
example, click the Connection Strings icon to open the Connection Strings pane:
Right-click the Connection Strings pane, and then click Add to add a new connection string using the Add
Connection String dialog box. The Add Connection String dialog box is a
user-friendly place in which to enter connection string information
(the following entry is a fictitious database name):
In addition to managing connection strings from
the Features View, you can also manage application settings. In the
Features View pane, click Application Settings to view the Application Settings pane, as shown in the following graphic:
In the Application Settings pane, right-click in the middle of the pane, and then click Add to open the Add Application Setting dialog box where you can add application settings—just as you did using the ASP.NET Web Site Administration Tool. Add a key/value pair like so:
Open web.config in your application. It should now include an entry for Copyright.
<?xml version="1.0" encoding="UTF-8"?>
<configuration >
<appSettings>
<add key="Copyright" value="Copyright © 2009" />
</appSettings>
</configuration>
Use the IIS ASP.NET configuration editor to add a setting named BackgroundColor with a value of #00FF00.
This exposes a setting that administrators can use to change the
background color of Default.aspx (after support for changing the
background color is built into the code). That is, anyone having access
to the web.config file will be able to modify the background color.
Return
to Visual Studio and add a property to the Default page
(Default.aspx.cs) to retrieve the background color. (If you created a
site using a master page, do this in the master page code file rather
than in Default.aspx.cs.) It should be available from the ConfigurationManager.AppSettings collection.
using System.Configuration;
public partial class SiteMaster : System.Web.UI.MasterPage
{
protected string BackgroundColor {
get { return
ConfigurationManager.AppSettings["BackgroundColor"]; }
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
By
default, Visual Studio will include a master page with your
application. Open the Site.master page in Source view and update the
<p> tag to retrieve the background color from the application settings. Use the <% and %> braces to mark executable code. Also add a line to the .aspx file to display the background color value.
<%@ Master Language="C#" AutoEventWireup="true"
CodeFile="Site.master.cs" Inherits="SiteMaster" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head runat="server">
<title></title>
<link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
</asp:ContentPlaceHolder>
</head>
<body style="background-color: <%=BackgroundColor%>">
<!-- other content is here... -->
</body>
</html>
Compile
the program and run the page. The value #00FF00 translates to a bright
green, so the background for the master page should now appear bright
green:
Browse
through some of the other icons in the ASP.NET Configuration Settings
pane in IIS. You will encounter many of these settings as you explore
ASP.NET in the coming chapters:
The Authentication page is for setting up users and assigning them roles in your application.
The .NET Globalization page manages globalization issues.
The
Session State management feature is for managing session state. You can
tell ASP.NET to store session state in any of a number of places,
including in process on the host computer, out of process using a
dedicated state server, or in a dedicated SQL Server database.
The Pages And Controls page allows you to manage the UI of your application such as themes and master pages.
ASP.NET relies on web.config
for almost all of its settings. The configuration story doesn't end
here, however. This chapter describes only a couple of settings, and
you will see most of them as well as many others throughout the book.
You revisit configuration when you encounter features such as security,
session state, error messages, and HttpHandlers/HttpModules.