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: ,

About wesmacdonald

Wes MacDonald is a Visual Studio ALM MVP, PSD, MCT and a Principal Consultant for LIKE 10 INC., a SharePoint Server, Visual Studio and Team Foundation Server Consulting Firm providing premium support and guidance services.

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: