Let's talk

Call us: +44 (0)1603 878240

How To Disable .NET Viewstate For Googlebot

11:53 on Mon, 17 May 2010 | Development | 5 Comments

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

Thanks Joe :)

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.

Comments & Discussion

(5 Comments)

Post a comment, your email will not be published, nor will it be harvested.

Clauber

Clauber • Last year

Was just looking for a simple solution to this! Cheers (2)

Reply

Mark

Mark • 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

Reply

Paul Thethi

Paul Thethi • 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.

Reply

Carl

Carl • Years ago

Was just looking for a simple solution to this! Cheers

Reply

Brighton Models

Brighton Models • 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!

Reply