19 - Global.asax file in ASP.NET

19.1 Introduction to Configuration files

The configuration data is stored in XML text files, as Web.config file. The file appears in multiple directories in ASP.NET applications. ASP.NET enables user to specify configuration settings that affect all web applications on a server.

The configuration files are grouped into sections that contain settings for individual features. The compilation configuration section is used to configure the settings of ASP.NET to compile the application. The attributes for the compilation are as mentioned below:

1) debug: User can set the debug attribute to true for generating the symbols required for debugging.

2) targetFramework: It specifies the version of .NET framework for the target website. It is included only for web sites that target the .NET framework 4 and later versions.

The syntax to add the configuration settings in the compilation section is as shown below:

<compilation debug=”true”
optimizeCompilations=”true”
targetFramework=”4.0” />

The Machine.config file and Web.config file are located in the .NET framework. The Machine.config file contains the default and machine specific values for all settings. The .config file contains XML as the root node of the configuration file. The section handler is a .NET framework class that implements the ConfigurationSection interface.

The declaration of the XML structure of Web.config file is as shown below:

<configuration>
  <configSections>
    <section name=”section1” type=”section1Handler” />
     <section name=”section2” type=”section2Handler” />
  </configSections>
  <section1>
    <s1Setting1 attribute1 = “attr1” />
  </section1>
  <system.web>
     <authentication mode=”Windows” />
  </system.web>
</configuration>

The configuration section settings area follows the configuration section handler area and actual configuration settings. The configuration section element contains child elements that are handled by the section handler as parent. The pages element contains a namespace element that has no corresponding section handler. The code snippet is as shown below:

<pages
  Buffer=”true”
  enableSessionState=”true”
  asyncTimeout=45”>
  <namepsaces>
    <add namespace=”System” />
    <add namespace=”System.Collections” />
  </namespaces>
</pages>

19.2 Custom Errors

The custom errors element can be defined at any level in the application file hierarchy. The syntax for custom errors is as shown below:

    <customErrors defualtRedirect=”url” mode=”On|off|RemoteOnly”>
     <error/>
   </customErrors>

The attributes in the custom errors are as mentioned below:

Attribute

Description

defaultRedirect

It is used to specify the default URL to direct a browser if an error occurs

Mode

It is used to specify whether custom errors are enabled, disabled or shown for remote clients.

On: If the custom errors are enabled

Off: If the custom errors are disabled

RemoteOnly: It specifies the custom errors to be shown to the remote clients

The child elements for the custom errors are as shown below:

Element

Description

error

It specifies the custom error page for the HTTP status code

The parent elements for the custom error are as shown below:

Element

Description

configuration

It specifies the root element in every configuration file used by the common language runtime and .NET framework applications

system.web

It specifies the root element for the ASP.NET configuration settings in a configuration file and contains configuration elements that web applications

The default customErrors element is configured in the Machine.config file. The syntax is as shown below:

     <customErrors mode=”RemoteOnly” />

19.3 Tracing

Tracing feature of ASP.NET is useful to trace the program execution ensuring the web application executes properly. User can view the diagnostic information about the web page. The information contains execution time of page methods, contents of various collections, HTTP header and session state.

The features provided by tracing are as mentioned below:

1) Integrated tracing functionality: User can route or forward the messages by System.Diagnostics.Trace class to the ASP.NET tracing output.

2) Debugging statements: User can write debug statements in the code without removing from the application when it is deployed from the production servers.

3) Programmatic access: User can access and change the trace messages from the code over the format of trace messages

4) Application level tracing: User can view most recent tracing data without restarting the tracing session

The tracing can be implemented at the following levels:

1) Page Level

2) Application Level

1) Page Level: It is used to generate the diagnostic information at the end of the page rendering.

It includes information about the page requests and responses in an application. To enable the tracing for a page add the following directive.

     <%@ Page Trace=”true” %>

User can include TraceMode attribute in the @Page directive to specify the trace messages. The messages can be sorted by time or category. The syntax to add the TraceMode attribute in the order in which they are processed is as shown below:

     <%@Page Language=”C#” Trace=”True” TraceMode=”SortByTime” %>

The syntax to enable tracing in a page and sort the trace messages by category is as shown below:

    <%@ Page Language=”C#” Trace=”True” TraceMode=”SortByCategory” %>

2) Application Level: It is used to trace the information for every web page in a web application. The trace.axd page is used to view the trace information. The page is accessible to the administrator only. To use the page user must enable application level tracing in the Web.config file. Add the <trace> element to change the trace settings of the website.

The attributes of the <trace> element are as shown below:

Attributes

Description

enabled

It indicates whether tracing is enabled in an application

requestLimit

It stores tracing information for the HTTP requests

pageOutput

It indicates whether the page information is displayed at the bottom of the page

traceMode

It indicates the order in which the trace messages are displayed

localOnly

It shows that the tracing information should be displayed on the local computer

mostRecent

It stores the most recent messages when set to true

The syntax for the trace element in the Web.config file is as shown below:

<configuration>
  <system.web>
    <trace
    enabled=”true”
    requestLimit=”100”
    pageOutput=”false”
    localOnly=true”
    mostRecent=”false” />
  </system.web>
</configuration>

19.4 Using the configSource attribute

The attribute is used to get or set the name of the include file associated with the configuration section. The ConfigSource property is used to represent the value of the configSource attribute in the ConfigurationSection object.

The ConfigurationObject implementation can be optionally specified in a separate file in which the configuration settings are defined. It is useful in multiple ways as mentioned below:

  1. User can include files resulting in logical and modular structure for configuration files
  2. File access security and permissions are used to restrict access to sections of configuration settings
  3. The settings in an include file not used during application initialization can be modified or reloaded without the application restart.

The syntax to include to add the attribute in the configuration file is as mentioned below:

   <pages configSource=”pages.config” />

Like us on Facebook