Creating ASP.NET Applications

Learn how to work with ASP.NET applications, including creating global variables and objects by storing information in application state, using the Global.asax file, configuring an application with the Web.Config and Machine.Config configuration files, and creating custom HTTP handlers and modules.

This chapter is from the book

In this chapter, you learn how to work with ASP.NET applications. First, you
learn how to create global variables and objects by storing information in application
state. Next, you learn how to use the Global.asax file, a special file
that represents the current application so that you can use it to handle application
wide events.

You also examine how to configure an application with the Web.Config
and Machine.Config configuration files. You examine the standard sections
of the Web.Config file so that you understand how to add new sections
of your own.

Finally, you learn how to create custom HTTP handlers and modules. You can
use a custom HTTP handler to perform custom logic when a request is made to
a certain page. A custom HTTP module can be used to perform custom logic on
each page request. At the end of this chapter, you learn how to create a WhosOn
application with a custom HTTP handler and module that display statistics on
the current users at your Web site.

Overview of ASP.NET Applications

An ASP.NET application consists of all files located within a particular
virtual directory. The virtual directory can be the default directory (the
wwwroot directory), or it can be a new virtual directory that you have
defined by using the Internet Services Manager.

Choose a physical directory for the virtual directory. It can be located
anywhere on your hard drive.

Choose the access permissions for the virtual directory. You need to
enable both Read and Run scripts to execute ASP.NET pages.

After you create a new application by creating a virtual directory, you can
access pages in the application by using URLs that look like this:

http://yourSite.com/myApp/default.aspx

This URL accesses a page located in the myApp application.

All the pages running in an application are isolated within an application
domain. Separating a Web site into multiple application domains, by creating
virtual directories, has a number of benefits:

Code that executes within one application cannot directly access code or
resources in another application. So, you can host multiple applications on a
single Web server without worrying about unintended interactions between the
different applications.

If one application is shut down (or it crashes), it has no effect on
other applications running on the same server. Therefore, flaky code executing
in a preproduction Web site can be isolated from code running on a live Web
site.

Assemblies in one domain are not shared with other domains. For example,
one application can use a different version of a component without any conflict
with other applications.

You can set application wide security policies. The code executing in a
trusted application domain can be subject to a different set of permissions than
code running in another untrusted application domain.

You can specify configuration settings that apply to one application and
not another. For example, you can enable tracing and debugging for one
application and not another application.

Every application can contain a special directory and two special files. The
special directory is named /bin. This directory must be located in the
root directory of the application.

NOTE

After you create a new virtual directory, you need to add the /bin
directory to the application yourself.

The /bin directory contains custom components and controls (in the
form of assemblies). Any component or control added to the /bin
directory is automatically visible to all pages executing within the
application. For example, if you create a new custom control named
SuperDataGrid, you need to place this control in the /bin
directory so that you can use it in your ASP.NET pages.

ASP Classic Note

In versions of Active Server Pages prior to ASP.NET, you needed to shut down
the current application before you modified any of the components in an
application. When using ASP.NET, in contrast, you can simply replace an old
component with the new component in the /bin directory.

An application can also contain a special file named Global.asax.
This file also must be located in the root directory of an application.

The Global.asax file contains subroutines that handle application
wide events and objects declared with application scope. You learn how to create
this file later in this chapter in the section titled "Using
the Global.asax File."

Finally, an application can contain in its root directory a Web.Config
file that specifies configuration information for the entire application. You
learn how to use this file later in this chapter in the section titled "Using
the Web.Config File."