Home » Development » How To Disable .NET Viewstate For Googlebot

How To Disable .NET Viewstate For Googlebot

by Phil Howard
17th May 2010 - 1 min read

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
End If
End Sub

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.

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn


Brighton Models / 7 years ago

Very useful tip. We've been struggling with that for a while and never thought that such a small piece of code would do the job. Thanks!

Carl / 7 years ago

Was just looking for a simple solution to this! Cheers

Paul Thethi / 7 years ago

Be careful using this method as this cloaking. Google also spiders your pages using a different ip address & is not called GoogleBot. You can be removed from the search engines for this.

Mark / 7 years ago

Hi Paul, This technique does not fall under cloaking. Cloaking is serving different *content* to users with the aim of deceiving the search engines. Changing the underlying code, has no effect. It's no different to changing CSS/layouts based on user-agent. Check out Google's page on this: http://bit.ly/2ZkU7G

Your email address will not be published. Required fields are marked *