Transfer Session Variables from Classic ASP to ASP.NET

Some time ago, I got into a bit of a research theme about figuring out how to interoperate Session state between classic ASP and ASP.NET. The reasoning was that a lot of developers have Classic ASP sites, and want to migrate to ASP.NET a piece at a time, and Session State transfer between them was the sticky issue.

The easiest way to explain this is to simply post the code, so here goes. These pages appear in the same order in which they are used, so just read through the code below:

' This is the page where we just set some Classic ASP Session Variables
' ASPPage2.asp is where the work is done.


' We graf all the session variable names/values and stick them in a form
' and then we submit the form to our receiving ASP.NET page (ASPNETPage1.aspx)...
For each Item in Session.Contents

<%@ Page language="c#" %>


<%@ Page language="c#" %>

As can be easily seen, all we need to do is grab the Classic ASP Session variables, contruct a dynamic form in a new Classic ASP page consisting of their names and values as hidden form fields, and submit it to our receiving ASP.NET page where we simply iterate the Form NameValueCollection , sticking the names and values into ASP.NET variables! You want to use Server.Transfer because its much more efficient than making another browser trip with Response.Redirect.

To make it more extensible, one of the Session variables, "DestPage" is used to tell us "where to go" when we're done with our little conversion. You would probably want to set this in the page that makes the call to the utility page, ASPPage2.asp. In this manner, you can use the two pages ASPPage2.asp and ASPNETPage1.aspx in almost any situation. And don't forget - you can reverse the process just as easily to transfer ASP.NET Session variables back to Classic ASP! As a last note, one reader commented about how you would handle the fact that somebody was in the ASP.NET portion of your site and meanwhile their Classic ASP session had expired. No problem! Since you brought all your session "baggage" with you when you came over, all of it (including any new stuff) would simply come back with you into a brand new ASP Session.

What about Objects and Arrays?

Objects are pretty much out of the question because Classic ASP has the most weakly typed script system around (or should I say, when everything is a variant, I wouldn't consider it a typing system). However, if you had an ADO Recordset in Classic ASP Session state, you could flatten it out into its XML Representation as a string by saving it with the Recordset.Save method into an ADODB.Stream object, read out the stream as text to, store that in a Session variable, and then easily decode it into a DataSet on the back end with SelectNodes("//z:row") if you had to. Arrays, including multidimensional arrays, as long as they can be flattened out into a string, can also be transferred. Just as a little "bonus" the downloadable zip below has code pages to do this process in both directions

Posted by:

Comments History

by: on
Session Variables was always my weak point.
by: on
by: on

Name :  

Email :  

Comment Below