10.1 Types of State


One of the most important decisions you face when designing a Web application is where to store your state. ASP.NET provides four types of state: application state, session state, cookie state, and view state. In this chapter, we explore each type of state, when it is most applicable , and any disadvantages you should be aware of if you decide to make use of it.

ASP.NET, like its predecessor, ASP, provides a pair of objects for managing application-level state and session-level state. Application state is where information that is global to the application may be stored. For efficiency, this state is typically stored once and then read from many times. Session state is maintained on a per-client basis. When a client first accesses any page in an application, an ASP.NET generated session ID is created. That session ID is then transmitted between the server and the client via HTTP either using client-side cookies or encoded in a mangled version of the URL (URL mangling is discussed in detail later in this chapter). On subsequent accesses by the same client, state associated with that session ID may be viewed and modified. Cookies provide the ability to store small amounts of data on a client's machine. Once a cookie is set, all subsequent pages accessed by the same client will transmit the cookie and its value.

Finally, view state is a yet another way of storing state on behalf of a client by saving and restoring values from a hidden field when a form is posted. Although this technique for retaining state has been used by Web developers in the past, ASP.NET provides a simplified mechanism for taking advantage of it. As we have seen in Chapter 8, it is possible to place items into the ViewState property bag available in every Page -derived class. When that page issues a POST request to itself, the values placed in the ViewState property bag can then be retrieved, the key restriction being that view state works only when a page posts to itself. Table 10-1 summarizes the advantages and disadvantages of each of the four types of state available in ASP.NET.

Table 10-1. State Type Comparison in ASP.NET

Type of State

Scope of State

Advantages

Disadvantages

Application

Global to the application

  • Shared across all clients

  • Overuse limits scalability

  • Not shared across multiple machines in a Web farm or processors in a Web garden

  • Primary purpose subsumed by data cache in ASP.NET

Session

Per client

  • Can configure to be shared across machines in a Web farm and processors in a Web garden

  • Requires cookies or URL mangling to manage client association

  • Off-host storage can be inefficient

Cookie

Per client

  • Works regardless of server configuration

  • State stored on client

  • State can live beyond current session

  • Limited memory (~4KB)

  • Clients may not support cookies or may explicitly disable them

  • State is sent back and forth with each request

View

Across POST requests to the same page

  • Works regardless of server configuration

  • State is retained only with POST request made to the same page

  • State is sent back and forth with each request



Essential ASP.NET With Examples in C#
Essential ASP.NET With Examples in C#
ISBN: 0201760401
EAN: 2147483647
Year: 2003
Pages: 94
Authors: Fritz Onion

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net