Lottery Post Journal

New ASP.NET technique for hybrid site

I finally finished the new Active Users page at Lottery Post, which gives a much more accurate count than the old page.  There are a couple of entries in the Change Log that talk about some of the issues I faced in rebuilding it.

As people reading this may be aware, I have been slowly transforming Lottery Post from a classic ASP site to a site based upon ASP.NET 2.0.  Although it is possible to run a "hybrid site" consisting of both technologies, it can be very difficult to manage, especially with a complex and sophisticated site like Lottery Post.

One of the biggest issues I face is that even though the classic ASP and ASP.NET pages run in the same web site, they do not share the same session object.  Therefore, the ASP.NET sessions tracker is not "aware" of page clicks on classic ASP pages (and vice-versa).

This issue has several deeper ramifications for Lottery Post, centering mainly around usability issues, but the Active Users page is the clearest way to demonstrate the need for a solution.

There is very little information out there on running hybrid applications using classic ASP and ASP.NET, and most of what I've seen requires complex custom code using SQL server to store session state, etc.  Over the past several months I've been mulling this around, trying to come up something more elegant.

I finally developed a very cool technique that gets the two environments talking to each other.  I haven't seen this technique used elsewhere, so I guess I'm the originator of it, at least for this particular issue.

In ASP.NET I created a special page which generates a 1-pixel image and writes the image to the output stream.  That special page also looks at the Request object's QueryString to take values passed into the page to do whatever manipulation is necessary in ASP.NET.  It could also look at the values in the cookie, since the cookie is passed to the server during an image request, but I didn't need to use the cookie in this case.

I'll refer to this special page as an "image-page".

In the classic ASP pages, I added an <img /> tag to every page, which uses the image-page URL in the src attribute, and passes a couple of values in the QueryString.

So, in effect, every classic ASP page is "calling home" to ASP.NET and sending whatever values it needs.  The 1-pixel image in every classic ASP is invisible to the user.

One other important detail is that in the QueryString of the <img /> tag I include a quasi-random number at the end, so that the classic ASP page does not use a cached version of the image-page.  (It will always retrieve a fresh copy from the server, because the URL is different every time.)  The added number I create is based upon the DateSerial() number, plus the Timer() number.  I prefer not to use Rnd() because I don't like the idea that Rnd() creates patterns of numbers, rather than numbers that are guaranteed to be different every time.

This is the framework of the ASP.NET page that captures the classic ASP session and returns a 1-pixel image.

<%@ Page ContentType="image/gif" %>
<%@ Import namespace="System.Drawing" %>
<%@ Import namespace="System.Drawing.Imaging" %>
<script runat="server">
     Sub Page_PreRender(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.PreRender
          ' Look at QueryString, processing... 
           Dim objBitmap As New Bitmap(objPage.MapPath("space.gif"))
          objBitmap.Save(objPage.Response.OutputStream, ImageFormat.Gif)
     End Sub



  • my kind of cookies are oreos - so i didn't understand a word. however - wish to thank you for working hard. em

    By emilyg, at 10:16 PM

  • Thank you and kudos Todd for your hard work. I can tell you feel good about your creation. Good Job!

    By Tenaj, at 1:48 PM

Post a Comment

<< Home