Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


ASP.NET Simplifies State Management in Web Applications : Page 5

If you're tired of writing state management code in your Web applications, you'll be happy to know that ASP.NET not only greatly simplifies the process, but also solves some of the classic ASP Session object's problems, such as using Sessions with Web farms, and persisting state data.

Using Cookieless Sessions
Sessions rely on cookies by default. Although all modern Web browsers support cookies, users may choose to turn off cookie support, or the client device (such as a WAP phone) may not support cookies. In these cases, the Session object will not work. You can work around this problem by embedding the Session ID in each URL. This technique is used widely by many Web sites, such as Amazon.com. ASP.NET now supports this "cookieless" method for supporting Sessions. To enable cookieless sessions, edit your application's web.config file and set the cookieless attribute to "true":

<sessionState mode="InProc" cookieless="true" timeout="20" />

After altering the web.config file, when you load Webform1.aspx, the URL will look similar to the following (the URL is a single line):

http://localhost/StateManagement/(ymqpoo45lyfih455lcf2 kvfp)/WebForm1.aspx

Note that the Session ID is now embedded within the URL, enclosed in parentheses. To request Webform2.aspx, change the URL to (all on one line):

http://localhost/StateManagement/ (ymqpoo45lyfih455lcf2kvfp)/WebForm2.aspx

You should see the time in which Webform1.aspx is requested. However, if you request Webform2.aspx without the embedded Session ID (http://localhost/StateManagement/WebForm2.aspx), the state is lost and the ASP.NET framework issues a new Session Id.

Note that you can't have it both ways—either you use cookie-based Sessions or cookie-less Sessions but not both. To determine if the browser supports cookies, try creating a cookie and examine the value of the cookie when a postback occurs. If the cookie value is found, that means cookies are supported on the browser.

Maintaining State in Hidden Fields
Yet another way to maintain state without using cookies is to use hidden form fields in your Web pages. You can use the HTMLInputHidden control (under the HTML tab in Toolbox) to add hidden fields (see Figure 8).

Figure 8: The HTML tab contains a Hidden control that inserts a hidden field in a Web Form.
To set a value using the HTMLInputHidden control, simply set its Value property:

HiddenField.Value = "some values here"

On postbacks, you can retrieve and examine the value of the hidden field.

hiddenValue = HiddenField.Value

Using the Session Object in a Web Farm
Microsoft has also added ASP.NET features that help solve the problems involved in using the Session object in a Web server farm. ASP.NET provides two potential solutions to this problem: StateServer and SQLServer session support.

The StateServer solution stores all the Session variables for the entire Web farm on a single server. All Web servers in a Web farm need to configure their applications to point to this StateServer machine. The StateServer runs as a Windows service—installed when Visual Studio.NET is installed. Previously, you had to obtain the ASP.NET Premium Edition to get the StateServer service). Run the StateServer service on the machine that you want to be the central repository for Session variables. To run the service, invoke the Services application (Start-> Programs -> Administrative Tools -> Services) and start the ASP.NET State Server service.

Next, edit the mode and stateConnectionString attributes of the <sessionStateSessionState> element of the web.config file:

<sessionState mode="StateServer" stateConnectionString="tcpip=" timeout="20" />

Table 1 summarizes the various values of the mode attribute.

Table 1: Effect of using different values for the mode attribute.




Disables Session state


The default. The Session state is "in process" with an ASP.NET worker process


Session state information is stored in SQL Server.


Session state information is stored in a State Server

The stateConnectionString attribute specifies the server identified as the State Server. The sample code above uses my local machine; so the IP address is the local loopback address The number 42424 is the port address of the service. Using the web.config settings shown, all session variables for the Web farm would now be routed and stored on this machine.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date