Friday, September 24, 2004

DNN and ViewState

Here are some tricks for modifying your DNN install to move the viewstate into a session. There are some give and take issues when doing this, but we cover it in this article.
I'm not a big fan of viewstate, great idea from getting away from sessions, but it really is a pain. MS is doing some enhancements to viewstate in the next version of ASP.NET, but being a DotNetNuke user I wanted to clean my DotNetNuke implementation from the viewstate. I know there are several methods of dealing with viewstate from loading into a file, database or session. For me, memory overhead of storing in a session wasn't as big of an issue so I opted for that.

Moving viewstate into a session is pretty easy to do in DotNetNuke. Since all pages inherit from the DotNetNuke.Basepage class, all you need to do is modify this one class in order to remove the viewstate from your HTML and put it into the session or server's memory. Below is the modification I did in order to do that, another thing you'll notice here is I took out the method that intercepts the request and places the viewstate into the bottom of the HTML form instead of the top. This method was added for spiders, since relevant content is indexed at the top of the HTML page, and considered less relevant when content is towards the end. Since some of the viewstate crypts were hovering around 50k, it was hurting DotNetNuke sites in their rankings. I really don't like the viewstate move method since it processes post-request, basically pulling out the viewstate from the HTML string and inserting it into the bottom of the page. There is plenty of overhead to do this on every request, and since I'm removing the viewstate crypt from the page it isn't necessary to have the method in there. This way I move the overhead onto the hardware which we can always upgrade if needed, and lighten the load on my bandwidth, and make the pages load much faster.

This is a core mod, so if you're going to use it you need to readd it if you upgrade your DotNetNuke install. The basepage.vb file is located in the components directory of the DotNetNuke application.

Imports System

Imports System.Diagnostics

Namespace DotNetNuke

Public Class BasePage

Inherits System.Web.UI.Page

Private _formatter As New LosFormatter

Private Sub Page_Error(ByVal Source As Object, ByVal e As System.EventArgs) Handles MyBase.Error
Dim exc As Exception = Server.GetLastError
Dim strURL As String = ApplicationURL()
If Not Request.QueryString("error") Is Nothing Then
strURL += "&error=terminate"
Else
strURL += "&error=" & Server.UrlEncode(exc.Message)
If Not IsAdminControl() Then
strURL += "&content=0"
End If
End If
ProcessPageLoadException(exc, strURL)
End Sub

'save viewstate to a session. in the next two methods we take the viewstate and load/unload into a session variable.

Protected Overrides Sub SavePageStateToPersistenceMedium(ByVal viewState As Object)
Dim Key As String = Request.Url.ToString & "__VIEWSTATE"
Dim memStream As New System.IO.MemoryStream
_formatter.Serialize(memStream, viewState)
memStream.Flush()
Session(Key) = memStream
End Sub

Protected Overrides Function LoadPageStateFromPersistenceMedium() As Object
Dim Key As String = Request.Url.ToString & "__VIEWSTATE"
If Not Session(Key) Is Nothing Then
Dim memStream As System.IO.MemoryStream = CType(Session(Key), System.IO.MemoryStream)
memStream.Seek(0, IO.SeekOrigin.Begin)
Return _formatter.Deserialize(memStream)
End If
End Function

End Class 'BasePage

End Namespace

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home