Tag Archives: asp.net

Outline of Viewstate in ASP.Net and how to utilize it for SEO


One of the innovative features that Microsoft introduced in its .Net platform is called ViewState which is automatically generated code, hidden from users, that stores a user’s current session data.

Viewstate adds some hidden fields to your website. Viewstate includes Meta data to the source code. For the reason that search engines, like Google, focus on the first part of the source code – so if the source code of your ASP.NET website begins with these hidden fields for viewstate, Google will have small problems to find out, what your page is about. As a final point, your page will suffer a bad ranking, even if your content is the best there is. Just because of unoptimized source code.

The complexity is that the viewstate data can really be quite lengthy. It’s tough to visualize what could possibly need that much code to store a user’s session. As per the recent assessment search engines will only read the first 100K of a web page. Whether you agree with the 100K measure, it’s sensible to expect that pages beyond some size will not be downloaded in full by search engines. If the ViewState code pushes a web page beyond this threshold, you have got a problem.

The further problem that some people might be concerned about is whether all of this code dilutes the page i.e. the code to text ratio is high.

The excellent information is that there are solutions to the ViewState problem. First off, in many cases the ViewState is not really needed, so just turn it off. When it is needed, there is a method for moving it to the bottom of the web page.

Use viewstate only if it is necessary

Viewstate is valuable, whenever a page posts back to itself. Whenever this does not happen, just deactivate viewstate.

The lack of the implementation is, that you may deactivate viewstate but ASP.NET will defiantly render a viewstate when a form exists with runat=server. So if you really want to keep viewstate out of your ASP.NET website AND want to use form runat="server" you can avoid viewstate  at all:

protected override void SavePageStateToPersistenceMedium(
object viewState)
{

}

protected override object LoadPageStateFromPersistenceMedium()
{

return null;

}

Use viewstate at the bottom of the source code

This is fundamentally the cleanest solution for all websites that use viewstate and want to optimize for Google and Yahoo:

protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
System.IO.StringWriter stringWriter = new System.IO.StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
base.Render(htmlWriter);
string html = stringWriter.ToString();
int StartPoint = html.IndexOf(" if (StartPoint >= 0) {
//does __VIEWSTATE exist?
int EndPoint = html.IndexOf("/>", StartPoint) + 2;
string ViewStateInput = html.Substring(StartPoint, EndPoint - StartPoint);
html = html.Remove(StartPoint, EndPoint - StartPoint);
int FormEndStart = html.IndexOf("") - 1;
if (FormEndStart >= 0) {
html = html.Insert(FormEndStart, ViewStateInput);
}
}
writer.Write(html);

}
Viewstate is so helpful – no uncertainty about it. Except the pages where it is really used and simply in a mode, that it does not hurt your search engine rankings.

Advertisements
%d bloggers like this: