How To Disable .NET Viewstate For Googlebot
Something we’ve been doing for a long time now is hiding .NET Viewstate from search engine crawlers. While .NET Viewstate is needed to maintain the state for users between postbacks, they can be absolutely massive and bloat out your source code.
It’s not uncommon to see .NET Viewstate that are 800, 900 or even over 1,000 characters. You really want to strip out these Viewstate to reduce the size of the page that Googlebot and other crawlers have to go through.
An Easy Solution
There’s a lot of complex solutions online on how to disable Viewstate for crawlers, but here’s a nice simple one. Simply add this code to your master page page load, so it runs for all pages:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
If Request.UserAgent.ToLower.Contains(“googlebot”) OrElse Request.Browser.Crawler Then Page.EnableViewState = False
N.B. .NET will still show a part of the Viewstate – but it will be truncated usually to around 50 characters, which isn’t going to cause any problems.