Home
    Shop
    Advertise
    Write For Us
    Affiliate
    Newsletter
    Contact

ASP.NET Configuration System

ASP.NET Configuration system is used to describe the properties and behaviors of various aspects of ASP.NET applications. Unlike Classic ASP where configuration information was stored in a binary repository called the IIS metabase, ASP.NET uses XML-based configuration system that is more accessible and easier to use.

 

You can configure features, such as Connection Strings, Authentication Modes, Caching, Debug and Tracing, Custom Errors and many more.

Benefits of XML-based Configuration files

  • ASP.NET Configuration system is extensible and application specific information can be stored and retrieved easily. It is human readable.

  • You need not restart the web server when the settings are changed in configuration file. ASP.NET automatically detects the changes and applies them to the running ASP.NET application.

  • You can edit Configuration file using simple text editor. It can be easily exchanged between servers in a typical web farm scenario.

Configuration Files

ASP.NET configuration data is stored in two primary XML-based files. These files allow you to easily edit configuration data at any moment even after the application is deployed on server.

Different types of Configuration files

Machine.config: Server or machine-wide configuration file

Web.config: Application configuration files which deal with a single application

Server Configuration file (Machine.config)

Every ASP.NET server installation includes a configuration file named machine.config, and this file is installed as a part of .NET Framework installation. You can find machine.config in C:\<Windows>\Microsoft.NET\Framework\\Config\

ASP.NET 2.0 provides another two files machine.config.default and machine.config.comments. The machine.config.default acts as a backup for the machine.config file. The machine.config.comments file contains a description for each configuration section and explicit settings for the most commonly used values.

Application Configuration file (Web.config)

Each and Every ASP.NET application has its own copy of configuration settings stored in a file called Web.config. If the web application spans multiple folders, each sub folder has its own Web.config file that inherits or overrides the parent's file settings.

Configuration File Format

Both Machine.config and Web.config share the same XML schema. Configuration files are divided into multiple sections, with each section being a top-level XML element. The root level element in a configuration file is always <configuration>. Configuration file is organized as hierarchy of section handlers, with each section provides a unique functionality.

For ex: <SessionState> section handler handles session state for the application.

Common Configuration Settings (Machine.config & Web.config)

We will discuss common section groups/settings present in the configuration files. Let’s start with Connection Strings section group.

Connection Strings

In ASP.NET 1.0/1.1, all connection string information was stored in the <appSettings> section. However, ASP.NET 2.0 introduces a new section called <connectionStrings> that stores all kinds of connection-string information.

<configuration>
    <connectionStrings>
      <add name ="CookieDemo"
           connectionString ="server=aras02;database=aras02_Db;
           uid=freelance91;pwd=freelance91"/>
    </connectionStrings>
 </configuration/>

Session State

You can configure session information using the <sessionState> element. ASP.NET 2.0 introduces new Session state mode called custom mode which allows developer to persist state in any permanent store like XML or databases like Oracle, DB2 using a custom written provider class.

<sessionState
         mode ="StateServer"
         cookieless ="false"
         timeout ="20"
         stateConnectionString="tcpip=aras02:42424"
         stateNetworkTimeout="60"
         sqlConnectionString =""
         />

Compilation Model Configuration

ASP.NET Compilation Settings can be configured using the <compilation> element. You can configure various options like debug assemblies, default language to use in dynamic compilation model and other compiler options like compiling custom resource files.

Custom Errors

When the ASP.NET application fails, the ASP.NET page can show the default error page with the source code and line number. We can prevent this kind of error messages by configuring <customErrors> element which allows for defining custom error messages in an ASP.NET application.

  <customErrors mode="[on/off/RemoteOnly]" defaultRedirect="[URL]">
    <error statusCode="[statusCode]" redirect="[URL]" />
  </customErrors>

Authentication

You can configure security model for your application using <authentication> element. ASP.NET supports three forms of authentication. They are:

  • Windows authentication

  • Passport authentication

  • Forms Authentication

You can disable authentication by setting mode attribute = "none"

Custom Application Specific settings

Every web application must store some application-specific information for its runtime use. The "appSettings" section provides a way to define custom application settings for an ASP.NET application.

  <appSettings>
    <add key="[key]" value ="[Value]"/>
  </appSettings >

Different ways of specifying ASP.NET Configuration

ASP.NET provides hierarchical model for specifying configuration data. Each lower level in hierarchy can override the settings defined in upper level in the hierarchy. It also inherits the settings from parent level in hierarchy.

Machine.config->Web.config (root folder) ->Web.config (sub directory)

  • Specify at the machine level(machine.config) which apply for all applications hosted on machine

  • Specify at the application level(web.config) which apply for single application(root directory level)

  • Specify at the sub directory level(web.config) which apply for application sub directory

In ASP.NET 1.0/1.1, Frame work provided API's that enabled you only to read information from the configuration file. You had no way to write information into the configuration file. You have to manually change settings in configuration files which is error prone due to XML tags, case-sensitivity

However ASP.NET 2.0 is shipped with API's which are capable to manipulate the configuration information settings in local machine or remote machine.

Different ways to create and Edit the Configuration files:

  • ASP.NET MMC Snap-in

  • Web site Administration tool

  • ASP.NET Configuration API (Programmatic configuration)

  • Text Editors (Visual Studio IDE)

Configuration files are based on XML, the elements that describe the configuration are case-sensitive.

ASP.NET MMC Snap-in

ASP.NET provides a snap-in tool for Microsoft Management Console (MMC) to handle configuration settings for the applications deployed on web server. This is a graphical tool to edit ASP.NET configuration files. To edit configuration information using snap-in tool

Go to Internet Information Services(IIS) Manager and right click the virtual directory of your application and select properties. Below screen is displayed


ASP.NET MMC Snap-in

On ASP.NET Tab, Click Edit Configuration button,ASP.NET Configuration Settings dialog button appears. To Add custom application specific settings, Use General Tab to add key/value pairs. Remaining Tabs are self-describing. This tabs are used to configure settings which we discussed under Common Configuration Settings section


ASP.NET Configuration Settings

Web site Administration Tool

ASP.NET Web site Administration Tool allows manage and edit the application configuration data using a simple web interface. This tool is included with Microsoft Visual Web Developer Web Development Tool. You should have read/write access permissions to web.config before we administer tool(Administrator privileges).

To access the Web Site Administration Tool, on the Website menu, click ASP.Net Configuration.


WebSite Administration Tool

ASP.NET Web site Administration Tool groups related configuration settings under each tab.

Security Tab

Security Tab allows to secure resources of website and to manage user accounts and roles. You can specify authentication mode for the application.

Application Tab

You can define application settings, which are name/value pairs which you want to maintain at a central location and access in code from any where in the website. You can also define Debug and Trace settings.

Provider Tab

Use the Provider tab to test or assign providers for membership and role management for the Web site By default, the Web Site Administration Tool configures and uses a local Microsoft SQL Server Standard Edition database in the App_Data folder for the Web site

Changes to configuration settings that you make in the Web Site Administration Tool take effect immediately. This requires the Web site to which the change applies to be restarted. This may cause active sessions to be lost.

ASP.NET Configuration API (Programmatic configuration)

ASP.NET Configuration API allows you to manage configuration data using programming interface. Using configuration API, you can edit configuration data programmatically with out directly editing the XML configuration files. All of ASP.NET Configuration API's are stored in the System.Configuration and System.Web.Configuration namespaces. These classes are newly introduced in ASP.NET 2.0. You can access the configuration data for web application using the WebConfigurationManager class.

//Retrieves Configuration data for a web application
System.Configuration.Configuration webconfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/CookieDemo");
webconfig.SaveAs("D:\\webconfig.xml");

In above sample, OpenWebConfiguration method opens and returns the configuration object for CookieDemo web application.

We have covered various new features of ASP.NET 2.0 Configuration system. New tools like WebSite Administration Tool and ASP.NET MMC snap-in provide GUI interface which help developers to implement the security features so easily for an application.


Tutorial toolbar:  Tell A Friend  |  Add to favorites  |  Feedback  |