View State is an ASP.NET technology that allows for different states of information to be stored. It is generally used in Web form pages that cause a subsequent server request for the same page, called a postback. This allows users to save previous searches on return visits. The downside is this:
<input type="hidden" name="__VIEWSTATE" value="dDwtMTU3ODMxMzE0NTt0PDtsPGk8MD47aTwx PjtpPDI+O2k8ND47PjtsPHQ8cDxsPFRleHQ7PjtsPENhcGl0YWwgRGVzaWduIC0gSG9tZTs+Pjs7Pjt0PHA8 bDxUZXh0Oz47bDxcPE1FVEEgTkFNRT0iZGVzY3JpcHRpb24iIENPTlRFTlQ9IiJcPjs+..." />>
Had the entire View State tag been included, this article would have been over three pages long. This is obviously a problem for SEO purposes because as this tag gets longer, the content of your site is encountered by spiders later and later in the code. There are a couple of methods that can be used to either move the View State to the end of the page, or to completely disable it, depending on how your server and your site are set up.
The first implementation — moving it to the bottom of your pages — is the least server-intensive method. A few lines of code are added to the server:
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter) Dim stringWriter As System.IO.StringWriter = New System.IO.StringWriter Dim htmlWriter As HtmlTextWriter = New HtmlTextWriter(stringWriter) MyBase.Render(htmlWriter) Dim html As String = stringWriter.ToString() Dim StartPoint As Integer = html.IndexOf("<input type=""hidden"" name=""__viewstate""") If StartPoint >= 0 Then 'does __viewstate exist? Dim EndPoint As Integer = html.IndexOf("/>", StartPoint) + 2 Dim viewstateInput As String = html.Substring(StartPoint, EndPoint - StartPoint) html = html.remove(StartPoint, EndPoint - StartPoint) Dim FormEndStart As Integer = html.IndexOf("</form>") - 1 If FormEndStart >= 0 Then html = html.Insert(FormEndStart, viewstateInput) End If End If writer.Write(html) End Sub
What this does is override the Render() method for the page and move the View State from the default location at the top of the page to the bottom of the page, resulting in better spidering.
Alternatively, you can implement the use of sessions instead of the View State. This is a little more server-intensive, but it may be worth considering if you have multiple controllers on one page that use the View State, and it could even result in faster performance in the long run. Basically what this does is “force” the View State off the page and places it on the server, thus overriding the SavePageStateToPersistenceMedium and LoadPageStateFromPersistenceMedium mechanisms that store and retrieve View State data. By doing this, the View State process is intercepted. In other words, you are taking the information stored in the View State and inserting it somewhere else, like a SessionID. The benefit of this is that your pages will be much lighter without the View State, and form data is still able to be saved and retrieved upon subsequent visits to a site. Also, spiderability of a Web page or site is increased since HTML coded pages don’t have to carry around the View State baggage, resulting in reduced page sizes and rendering times.