asp.net [foreword:]asp.net is the latest technology for developing web-based applications from Microsoft. It provides a number of benefits over traditional ASP scripting technologies, including:
(1) A better development structure is established by separating the UI presentation layer (presentation) from the commercial logic (business logic);
2 replaces traditional ASP code translation with fully compiled code;
3 It compiles features in conjunction with each supported method, which means that sites using ASP.net are more performance-efficient than traditional ASP sites.
While there are many potential benefits to converting existing ASP applications to ASP.net, some ASP application tasks are important and complex. The conversion process may require additional resources and pose additional risks to the application. One way to solve these problems is to run both ASP and ASP.net applications and convert a conversation to asp.net at one time. In order to run both old and new programs at the same time, it is necessary to establish a mechanism to share the state of conversation between traditional ASP and asp.net. This article discusses how to use. NET Framework components share state information with the class and serialization attributes.
Overview
Cookies are the most common way for Web applications to identify user conversations and can be used to identify traditional ASP and ASP.net dialog states. In ASP scripts, state information is stored in memory and cannot be shared with other applications, such as asp.net. If the dialog state is saved in a common format in Microsoft SQL Server, it can be accessed by traditional ASP and asp.net.
In this case, a cookie named mysession is used to identify user conversations. When a user makes a request to a Web application, a unique cookie is generated for that user to identify the conversation. In a subsequent request, the browser sends the unique cookie back to the server to identify the conversation. A custom object will use the unique cookie to reload user dialog data from SQL Server before the requested Web page is loaded. The dialog status in the Custom Object Web page is accessible. When the Web request completes, the dialog data is saved back to SQL Server if the request terminates (see Figure 1).
Figure 1. Simple Data Flow Diagram
Asp. NET implementation
Each Web page in asp.net is derived from the System.Web.UI.Page class. The page class sets up an instance of the HttpSession object to process the dialog data. In this case, the custom page class, called Sessionpage, derives from System.Web.UI.Page, providing all the attributes similar to the page class. The only difference is that the default HttpSession uses a custom dialog object overload (using the new modifier for instance variables, C # allows derived classes to hide the members of the base class).
public class SessionPage:System.Web.UI.Page
{
...
Public new MySession session = NULL;
...
}
The custom dialog class uses the HybridDictionary object to save the dialog state in memory appropriately (HybridDictionary can be used to handle any number of dialog elements). For common use with traditional ASP, the custom Dialog object restricts the dialog data type to a string type (the default httpsession allows dialogs to hold any type of data, not common to traditional ASP).
[Serializable]
public class MySession
{
Private HybridDictionary dic = new HybridDictionary ();
Public MySession ()
{
}
public string This [string name]
{
Get
{
Return (String) dic[name. ToLower ()];
}
Set
{
Dic[name. ToLower ()] = value;
}
}
}
The page class exposes different events and methods for customization. In particular, the OnInit method is used to set the initialization state of the Page object. If the request does not contain a cookie named MySession, a new mysession cookie will be generated for the requester. In addition, the dialog data is retrieved from SQL Server using the custom data Access Object Sessionpersistence. The DSN and sessionexpiration values are retrieved from the web.config.
Override protected void OnInit (EventArgs e)
{
InitializeComponent ();
Base. OnInit (e);
}
private void InitializeComponent ()
{
Cookie = this. Request.cookies[sessionpersistence.sessionid];
if (cookie = null)
{
session = new MySession ();
Createnewsessioncookie ();
Isnewsession = true;
}
Else
Session = Sessionpersistence.loadsession (
Server.urldecode (cookies. Value). ToLower (). Trim (),
Dsn
Sessionexpiration
);
This. Unload + = new EventHandler (this. Persistsession);
}
private void Createnewsessioncookie ()
{
cookie = new HttpCookie (Sessionpersistence.sessionid,
Sessionpersistence.generatekey ());
This. RESPONSE.COOKIES.ADD (cookie);
}
The Sessionpersistence class uses the BinaryFormatter of the Microsoft. NET Framework component to serialize and parallel the dialog state in binary format to provide optimal performance. The resulting binary dialog data is then stored as an Image field type in SQL Server.
Public mysession loadsession (string key, String DSN,
int sessionexpiration)
{
SqlConnection conn = new SqlConnection (DSN);
SqlCommand loadcmd = new SqlCommand ();
loadcmd.commandtext = command;
Loadcmd.connection = conn;
SqlDataReader reader = null;
MySession session = NULL;
Try
{
LOADCMD.PARAMETERS.ADD ("@ID", New Guid (key));
Conn. Open ();
reader = Loadcmd.executereader ();
if (reader. Read ())
{
DateTime lastaccessed =reader. GetDateTime (1). AddMinutes (sessionexpiration);
if (lastaccessed >= datetime.now)
Session = Deserialize ((byte[]) reader["Data");
}
}
Finally
{
if (reader!= null)
Reader. Close ();
IF (conn!= null)
Conn. Close ();
}
return session;
}
Private mysession Deserialize (byte[] state)
{
if (state = null) return null;
MySession session = NULL;
Stream stream = null;
Try
{
stream = new MemoryStream ();
Stream. Write (state, 0, state.) Length);
Stream. Position = 0;
IFormatter formatter = new BinaryFormatter ();
Session = (mysession) formatter. Deserialize (stream);
}
Finally
{
if (stream!= null)
Stream. Close ();
}
return session;
}
At the end of the request, the Unload event for the page class is started, and an event-handling method registered with the Unload event will serialize the dialog data into binary format and save the resulting binary data to SQL Server.
private void Persistsession (Object obj, System.EventArgs arg)
{Sessionpersistence.savesession (
Server.urldecode (COO