ASP.NET 2.0 ViewState and SEO (Search Engine Optimization)


You may have some pages which are being penalized by certain search engines due to the size of the viewstate embedded in the page.  You can move the viewstate into the session easily by overriding the following methods:

void SavePageStateToPersistenceMedium(object viewstate)

object LoadPageStateFromPersistenceMedium()

I recommend you override these methods in a base page class of some kind so that you can just inherit your page from it.  The code I have used in the past is below:

using System;
using System.Data;
using System.IO;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public class MyBasePage : Page
    {
        public MyBasePage()
        {
        }

        private LosFormatter _LosFormatter;
        protected override void SavePageStateToPersistenceMedium(object viewState)
        {
            string Key = Request.Url.ToString() + "__VIEWSTATE";
            System.IO.MemoryStream memStream = new System.IO.MemoryStream();
            _LosFormatter.Serialize(memStream, viewState);
            memStream.Flush();
            Session[Key] = memStream;
        }

        protected override object LoadPageStateFromPersistenceMedium()
        {
            string Key = Request.Url.ToString() + "__VIEWSTATE";
            if (Session[Key] != null)
            {
                System.IO.MemoryStream memStream = (System.IO.MemoryStream)Session[Key];
                memStream.Seek(0, SeekOrigin.Begin);
                return (object)_LosFormatter.Deserialize(memStream);
            }
            return null;
        }
    }

 

Technorati Tags: ,
Wes MacDonald's avatar

About Wes MacDonald

Wes MacDonald is a DevOps Consultant for LIKE 10 INC., a DevOps consulting firm providing premium support, guidance and services for Azure, Microsoft 365 and Azure DevOps.

No comments yet... Be the first to leave a reply!

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.