Table Of ContentDynamic Web Programming
BUILDING WEB APPLICATIONS USING
ASP.NET, AJAX AND JAVASCRIPT
AGENDA
6. ASP.NET Application and State Management
6.1 Anatomy of an ASP.NET Application
6.2 ASP.NET configuration
6.3 ASP.NET State Management
6.4 ASP.NET View State
6.5 Query String
6.6 Cookies
6.7 Session State
Building Web Applications Using ASP.NET, AJAX And JavaScript
6. ASP.NET APPLICATION AND STATE
MANAGEMENT
94
6.1 ANATOMY OF AN ASP.NET APPLICATION
End user does not run an ASP.NET application directly! User
requests page, the web server passes request to IIS.
IIS or ASP.NET worker process associates requested page with
application domain depending on virtual directory.
Application domain is boundary enforced by the CLR to ensure
that one application cannot influence (see in-memory data) of
another.
All web pages in a single web application share:
Same in-memory resources, such as global app data, per user session
data, and cached data.
Same configuration settings (web.config file!)
95
6.1 ANATOMY OF AN ASP.NET APPLICATION
All web applications raise global application events at various
stages (application_start, application_end, application_error).
Use global.asax file in virtual directory to react to global
application events.
Virtual directory is basic grouping structure the delimits an
ASP.NET application. Application may consists of:
Web pages (aspx files)
Web services (asmx files)
Code files (cs files)
Configuration file (web.config file)
Global.asax file
Other components, such as compiled assemblies (ddl files)
Application Lifetime
ASP.NET uses lazy initializing techniques (meaning app domain
is created the first time a page is requested).
96
6.1 ANATOMY OF AN ASP.NET APPLICATION
Application shuts down when the server itself goes down. More
commonly, application restarts in a new app domain when an
error occurs, or when configuration or source files are changed.
Application Updates
Remarkable feature: You can update the web application without
restarting the web server. Old requests are still valid and
application operates using the old files, whereas the updated
application is created in a new domain.
ASP.NET uses shadow copy, a process that takes places during
compilation and copies all files into a temporary directory from
which the application is being run and files are locked.
ASP.NET tracks changes to its source files in order to trigger this
process.
97
6.1 ANATOMY OF AN ASP.NET APPLICATION
Application Directory Structure
Directory Description
Bin This directory contains all the precompiled .NET assemblies (usually DLLs) that the ASP.NET web application uses. These
assemblies can include precompiled web-page classes, as well as other assemblies referenced by these classes. (If you’re
using the project model to develop your web application in Visual Studio, rather than the more common website model, the
Bin directory will also contain an assembly that has the compiled code for your entire web application. This assembly is
named after your application, as in WebApplication1.dll.
App_Code This directory contains source code files that are dynamically compiled for use in your application. These code files are
usually separate components, such as a logging component or a data access library. The compiled code never appears in the
Bin directory, as ASP.NET places it in the temporary directories used for dynamic compilation. (If you are using the project
model to develop your web application in Visual Studio, rather than the more common website model, you do not need to
use the App_Code directory. Instead, all the code files in your project are automatically compiled into the assembly for your
web application alongside your web pages.)
App_GlobalResources This directory stores global resources that are accessible to every page in the web application.
App_LocalResources This directory serves the same purpose as App_GlobalResources, except these resources are accessible for their dedicated
page only.
App_WebReferences This directory stores references to web services that the web application uses. This includes WSDL files and discovery
documents.
App_Data This directory is reserved for data storage, including SQL Server Express or MS Access database files and XML files. Of
course, you are free to store data files in other directories.
App_Browsers This directory contains browser definitions stored in XML files. These XML files define the capabilities of client-side browsers
for different rendering actions. Although ASP.NET does this globally (across the entire computer), the App_Browsers folder
allows you to configure this behavior for separate web applications.
App_Themes This directory stores the themes used by the web application.
98
6.1 ANATOMY OF AN ASP.NET APPLICATION
Global.asax Application File
One global.asax file per application, must reside in root of virtual
folder.
Global.asax file is a code file only, no HTML or other tags.
This file cannot be requested by the end user.
The initial global.asax file only contains the most common
events, add other ones by simply typing the event framework
and the corresponding code.
Used to write event handlers that react to global events:
Events that occur for every page request
Events that occur under certain conditions (such as an error)
99
6.1 ANATOMY OF AN ASP.NET APPLICATION
Request and Response related events (page request)
Event Description
Application_BeginRequest() This method is called at the start of every request.
Application_AuthenticateRequest() This method is called just before authentication is performed. This is a jumping-off point for
creating your own authentication logic.
Application_AuthorizeRequest() After the user is authenticated (identified), it is time to determine the user’s permissions. You can
use this method to assign special privileges.
Application_ResolveRequestCache() This method is commonly used in conjunction with output caching. With output caching, the
rendered HTML of a web form is reused, without executing any of your code. However, this event
handler still runs.
At this point, the request is handed off to the appropriate handler. For example, for a web form request, this is the point when the page is
compiled (if necessary) and instantiated.
Application_AcquireRequestState() This method is called just before session-specific information is retrieved for the client and used to
populate the Session collection.
Application_PreRequestHandlerExecute() This method is called before the appropriate HTTP handler executes the request.
At this point, the appropriate handler executes the request. For example, if it is a web form request, the event-handling code for the page is
executed, and the page is rendered to HTML.
Application_PostRequestHandlerExecute() This method is called just after the request is handled.
Application_ReleaseRequestState() This method is called when the session-specific information is about to be serialized from the
Session collection so that it’s available for the next request.
Application_UpdateRequestCache() This method is called just before information is added to the output cache. For example, if you’ve
enabled output caching for a web page, ASP.NET will insert the rendered HTML for the page into
the cache at this point.
Application_EndRequest() This method is called at the end of the request, just before the objects are released and
reclaimed. It’s a suitable point for cleanup code.
100
6.1 ANATOMY OF AN ASP.NET APPLICATION
Description:ASP.NET application. Application may consists of: ❑ Web pages (aspx files)
error occurs, or when configuration or source files are changed. using the
project model to develop your web application in Visual Studio, rather than the
more common website model, . Set up custom settings for individ