ASP.NET SEO Quick Tip – Moving SEO Unfriendly Code To the Bottom of the Page

- August 25, 2008

A common SEO technique is to make sure the content of your web page is placed as close to the top of your HTML as possible. This will help ensure that your relevant content achieves higher priority by search engine spiders. ASP.NET provides a great framework for developing feature rich web applications, especially with the addition of view state. View state gives web forms the ability to persist changes across postbacks. Other web scripting languages are not able to accomplish this easily, however, this benefit may have some negative SEO implications.

The view state of a page is placed by default in a hidden form field named ___VIEWSTATE at the top of the html source code. The contents of the __VIEWSTATE form field contain serialized information, which can get very large (tens of kilobytes), about various controls on the web page. When a web page does not have a lot of controls using view state, the hidden form field will look something like this…


… which is probably fine at the top of the page. But, often times a web page may have numerous controls, no matter how much it is optimized to minimize view state, which produce a view state value that looks something like this…


… actually it could go on and on. This particular view state sample (this is just a small portion) was 10 pages long! Needless to say, you don’t want that to appear before your precious web page content.

There is an easy way to move the __VIEWSTATE form field to the bottom of the html source code. By pasting the following VB.NET code, “as is”, into your web form, the view state will be moved to the bottom of the html source code right above the closing </form> tag…

Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)<br />
Dim stringWriter As System.IO.StringWriter = New System.IO.StringWriter<br />
Dim htmlWriter As HtmlTextWriter = New HtmlTextWriter(stringWriter)<br />
MyBase.Render(htmlWriter)<br />
Dim html As String = stringWriter.ToString()<br />
Dim StartPoint As Integer = html.IndexOf("<input /><br />
If StartPoint >= 0 Then<br />
Dim EndPoint As Integer = html.IndexOf("/>", StartPoint) + 2<br />
Dim viewstateInput As String = html.Substring(StartPoint, EndPoint - StartPoint)<br />
html = html.Remove(StartPoint, EndPoint - StartPoint)<br />
Dim FormEndStart As Integer = html.IndexOf("") - 1<br />
If FormEndStart >= 0 Then<br />
html = html.Insert(FormEndStart, viewstateInput)<br />
End If<br />
End If<br />
writer.Write(html)<br />
End Sub

Now when you browse your web page, the __Viewstate hidden form field and its ridiculously long value, will be at the bottom of the page, and your precious content will be closer to the top, just how the search engines like it.

Comments are closed at this time.

© 2016 MoreVisibility. All rights reserved