Subscribe: Manuel Abadia's ASP.NET stuff
Added By: Feedage Forager Feedage Grade B rated
Language: English
adapter  control  field  hidden field  hidden  net  page  runat server  runat  server  state hidden  state  view state  view  xhtml 
Rate this Feed
Rating: 5 starRating: 5 starRating: 5 starRating: 5 starRating: 5 star
Rate this feed 1 starRate this feed 2 starRate this feed 3 starRate this feed 4 starRate this feed 5 star

Comments (0)

Feed Details and Statistics Feed Statistics
Preview: Manuel Abadia's ASP.NET stuff

Manuel Abadia's ASP.NET stuff

Updated: 2008-12-23T00:37:30.376375+01:00


Search Engine Optimization - Moving View State to the bottom of the page


One of the greatest problems when trying to optimize an ASP.NET page to be more search engine friendly is the view state hidden field. Most search engines give more score to the content of the firsts thousands of bytes of the document so if your first 2 KB are view state junk your pages are penalized. So the goal here is to move the view state data as down as possible. I have seen some approaches to solve this problem rewriting the final HTML code of the response. While this approach works I think that it wastes some precious processor cycles that can be used to do other things. So I needed a way to do the same thing without wasting that CPU time. After some large reflector sessions I found a way to do it. My method uses the ASP.NET Control Adapter Architecture. A control adapter is a class that can be used to control the HTML generated by the control it adapts. Since the Page class is the responsible of rendering the view state hidden field (Page.BeginFormRender calls Page.RenderViewStateFields), an adapter for the Page is needed. However, the view state hidden field plays a key role in the ASP.NET infrastructure (for example, the Page.IsPostBack property checks if the view state hidden field has been posted) and it is difficult to modify the associated HTML. A PageAdapter has a method called GetStatePersister() that returns an object that inherits from PageStatePersister. The PageStatePersister is called when it is time to load and save the view state. There are 2 classes that inherit from PageStatePersister: HiddenFieldPageStatePersister and SessionPageStatePersister. The first one is the default, which stores the view state in the hidden field called __VIEWSTATE. The second one stores the view state in the session. So, we can easily create a custom PageStatePersister to control the view state load and save process. The big problem is how to create the hidden view state field before the closing form tag while being a fully transparent solution. After some tries I came up with a solution that I was happy with. I realized that it was impossible to completely remove the view state hidden field from the top of the page, because it plays a key role in the ASP.NET infrastructure. However, with any custom page state persister the ASP.NET infrastructure renders at least an empty view state hidden field of only 70 bytes:   My page adapter adds a hidden field to the bottom of the form called __SEOVIEWSTATE with the actual view state data, and the only limitation that it has it is that you can not use expressions directly inside the form. However, this restriction can be easily avoided putting the expression in a PlaceHolder control or inside another control. For an in-depth explanation of this limitation take a look here. Let’s see an example of the adapter in action. The following ASP.NET page: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SEOViewStateAdapterTest._Default" %>        
        Enter a message:

Page generated at <%= DateTime.Now.ToString("hh:mm dd/MM/yyy") %>

XHTML Validation Module Enhancement


World Rally emulation completed


Playable status reached


More World Rally news


World Rally, finally


Google and AdSense