Technical Articles

Sharing Session State

ASP and ASP.net applications maintain their own session state this can be a problem for developers wishing to migrate from old style asp to take advantage of the powerful new functionality available in the Dotnet environment.  There are a number of ways around this problem, we are going to look at one very simple method.

If you have an existing ASP application which utilises the session object a simple method of transferring all the asp session details to an ASP.Net application is using an image tag with the src set to a page in the ASP.Net application (or equally effective a script element).  This code should be included in every page in the site that may alter the session state (ideally at the end of the page).

<IMG src='/sessionshare/dynimage.aspx?<%
  dim sItem, i
  i= 0
  For each sItem in Session.Contents
    if i <> 0 then
      Response.Write("&")
    end if
    Response.Write(sItem & "=" & Server.UrlEncode(Session.Contents(sItem))) 
    i= i + 1
  next %> height=1 width=1 style='display:none'>

The above code will be write an image tag to the page, which will not be displayed.  You may prefer to use the more widely supported class attribute and assign the display property to none in a stylesheet, rather than using the style attribute as above.  The page "/sessionshare/dynimage.aspx" must be created within your asp.net application and should have the following code in the Page_Load event.  (Code sample is in C#, but could be easily converted to VB.net)

private void Page_Load(object sender, System.EventArgs e) {
  for(int i=0; i<Request.QueryString.Count; i++) {
    if(Request.QueryString[i] == null) {
      Session[Request.QueryString.GetKey(i)] = "";
    } else {
      Session[Request.QueryString.GetKey(i)] = Request.QueryString[i].ToString();
    }
  }
  Response.ContentType = "image/gif";
}

This page loops through the session variables passed to it in the QueryString object and populates the ASP.Net session object with all of the session variables held in the ASP session object.

So there is a simple way to share your ASP sessions with a new ASP.net application.  You should ideally also use the same technique to transfer Asp.Net session variables in the other direction to your ASP application, ensuring that changes made to session state from within ASP.net are reflected in asp.  To do this complete the same process as above, by outputting an image tag with all your asp.net files that has it's source attribute set to an asp file, again passing the contents of the session object in the querystring.

Please note this is just one method of achieving the required result, the advantage of this approach is that doesn't require changes to the way the session object is accessed in either ASP or ASP.net pages.  A more in depth approach to this problem, which uses a custom session object to share information, perhaps providing a more suitable solution for larger scale projects is discussed on Microsoft's site at :

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/converttoaspnet.asp

 

Specialists in designing and building websites

Print this Page
Copyright © 2003-2022 - 3internet Ltd. 61 Highfield Lane, Tyttenhanger, St Albans, AL4 0RJ
Registered Company No. 4652486, England & Wales. VAT Registered No. 811 7329 46.