WEBSITE

Microsoft ASP.NET 4 : Configuring ASP.NET from IIS

10/12/2010 3:30:38 PM
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

  1. 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:



  2. 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:



  3. 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:



  4. 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:



  5. 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>

  6. 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.

  7. 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)
    {
    }
    }

  8. 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>

  9. 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:



  10. 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.

Other  
 
Most View
Samsung Ativ Smart PC Review (Part 1)
Managing Windows Server 2012 (part 8) - Using the System console
Windows Server 2008 and Windows Vista : Advanced Group Policy Management - Change Management
How To Buy…Network Attached Storage (Part 1)
Dell XPS One Touch 27 - An All-In-One Laptop Which Can Make Us Love Windows 8 (Part 4)
Cutting Edge Technology (Part 3)
Why Is Nobody Buying PCs?
Schenker XMG A722 – Powerful Gaming Laptop
Manage Add-Ons For Internet Explorer (Part 1)
101 Recommended Apps (Part 11)
Top 10
Sharepoint 2013 : Farm Management - Disable a Timer Job,Start a Timer Job, Set the Schedule for a Timer Job
Sharepoint 2013 : Farm Management - Display Available Timer Jobs on the Farm, Get a Specific Timer Job, Enable a Timer Job
Sharepoint 2013 : Farm Management - Review Workflow Configuration Settings,Modify Workflow Configuration Settings
Sharepoint 2013 : Farm Management - Review SharePoint Designer Settings, Configure SharePoint Designer Settings
Sharepoint 2013 : Farm Management - Remove a Managed Path, Merge Log Files, End the Current Log File
SQL Server 2012 : Policy Based Management - Evaluating Policies
SQL Server 2012 : Defining Policies (part 3) - Creating Policies
SQL Server 2012 : Defining Policies (part 2) - Conditions
SQL Server 2012 : Defining Policies (part 1) - Management Facets
Microsoft Exchange Server 2010 : Configuring Anti-Spam and Message Filtering Options (part 4) - Preventing Internal Servers from Being Filtered