Introduction
The point of this class is to help with migration of large ASP projects to ASP.NET.
The main purpose is to allow the developer to pass session like information back and forth between ASP.NET and ASP pages. I use this class in conjunction with a couple of other classes which I included in the source code. One is a persistable collection, which is really nothing more than a collection that I hold in viewstate.
Background
First, let me explain my reasoning for creating this class. Recently, I was given the task of upgrading a rather large ASP application to an ASP.NET application, but there were a couple of catches (always are).
- The updates had to go in phases.
- All of the pages pass certain values around to all other pages (via JavaScript client side posts).
- Sessions could not be used because multiple browser windows might sometimes be used, causing overwriting of session variables.
To elaborate, every link, button, etc. called a function that the client side posted to either the current page, or another page, and passed hidden form field values (I didn't design it!). The phased development approach made it so I couldn't rewrite the entire site to be ASP.NET, instead I had to do a few pages at a time based on requirements. So I sat down and thought it out and came up with this transition class.
Design
This class was made specifically for this particular web application, so that way you may or may not need to use certain things I've included with it. For example, instead of using viewstate directly, I created a class I call a hidden list. Basically all this class is, is a collection that is viewstate enabled so it will persist through postbacks. I mention this because one of the overloaded add methods for the page transition class allows me to pass it the entire hidden list, allowing me to add everything I need to pass in one shot.
Dim objTrans as New PageTransition("somepage.asp")
objTrans.Add(myHiddenList)
objTrans.Go
I will touch on the hidden list briefly, but it will be included in the source for you to examine.
Code - .NET Side
Private _objPassedValues As Hashtable
Private Sub SaveSessionState()
Dim strSessionID As String = HttpContext.Current.Session.SessionID.ToUpper
Dim intRnd As Integer = (New Random).Next
strSessionID += intRnd.ToString
HttpContext.Current.Session.Clear()
For Each strKey As String In _objPassedValues.Keys
Dim objConn As New SqlConnection(GenerateSqlConnString)
Dim objCmd As New SqlCommand("spSetSessionState", objConn)
objCmd.CommandType = CommandType.StoredProcedure
objCmd.Parameters.Add(New SqlParameter("@SessionID", strSessionID))
objCmd.Parameters.Add(New SqlParameter("@Variable", strKey))
If TypeOf _objPassedValues(strKey) Is Boolean Then
objCmd.Parameters.Add(New SqlParameter("@Value", _
_objPassedValues(strKey).ToString))
Else
objCmd.Parameters.Add(New SqlParameter("@Value", _
_objPassedValues(strKey)))
End If
objConn.Open()
objCmd.ExecuteNonQuery()
objConn.Close()
Next
If _strDestination.IndexOf("?") <> -1 Then
_strDestination += "&TranSID=" & strSessionID
Else
_strDestination += "?TranSID=" & strSessionID
End If
End Sub
As you can see, I got the main idea of this from cookieless sessions with SQL Server. (The stored proc is included in the source also.)
Basically what I do here, is this:
- Get the current session ID.
- Get a random number and append it to the session ID.
- Immediately clear the session to force a new session ID to be generated.
This keeps the ID unique for this browser. Then all it does is simply append the destination specified with a querystring value.
The class doesn't Response.Redirect
until you execute PageTransition.Go
. The reason for this is, because there is a method called SimulateGo
which instead of actually transitioning, does everything else and returns where it would have gone. You may think I'm nuts, but I actually had a reason for it.
Here's what the Go
method looks like:
Public Sub Go()
Dim objSession As SessionState.HttpSessionState = HttpContext.Current.Session
For Each strVariable As String In objSession.Keys
Try
_objPassedValues.Add(strVariable, objSession.Item(strVariable))
Catch
End Try
Next
SaveSessionState()
HttpContext.Current.Response.Redirect(_strDestination)
End Sub
As you can see, it's fairly simple. It grabs all values out of session at the last second (since they're cleared in SaveSessionState
) and then saves the session to the database. And lastly it redirects.
Code - ASP Side
This code is fairly simple. In our case, we had a dictionary object on every ASP page that held all of the form values (don't ask me why, not my design!). So I simply put any transitioned information directly into that, but you can do as you see fit.
Sub subHandleDotNetTransition
Dim strTransitionID, objTransitionRS, strReferer
strReferer=Request.ServerVariables("HTTP_REFERER")
if instr(strReferer,"?") > 0 then
strReferer= left(strReferer, instr(strReferer,"?")-1)
end if
If UCASE(Right(strReferer, 5)) = ".ASPX" Then
strTransitionID = Request.QueryString("TransID")
If strTransitionID <> "" Then
End If
End If
End Sub
In my case, this code was executed in a header file that was included on every page in the site.
Code - Hidden List
I still think the hidden list is a neat little tool. Anyway, here're the basics of it.
It's simply a hashtable, that I turned into a control, that has viewstate enabled.
What does that mean? Well, it means that I can store any serializable object in it, and it's preserved till I leave the page. (No more hidden variables!) I've included the source for it, but I won't bother posting its code, it's very simple really.
Conclusion
This was certainly a learning experience for me, but all in all, it worked rather well so I'm pleased. I hope this helps anyone else out as much as it's helped here. This is my first article, so please be kind. Also, I'm on here pretty frequently, so feel free to ask any questions or anything, I'll get back to you rather quickly.
History
- v1.0 - 2005.06.29 - Created.