How to Improve Search Engine Ranking or SEO of an ASP.NET Page

How to Improve Search Engine Ranking or SEO of an ASP.NET Page thumbnail
Search Engine Optimization in ASP.Net

Search engines such as Google, Yahoo and MSN only read the first X bytes of text when spidering your page. Move the ViewState block to the bottom of your form to improve your chances of having the real content and keywords of your ASP.Net page indexed and ranked. This article provides the code that you can drop into your base page class to implement this functionality.

Things You'll Need

  • Visual Studio .Net 2003 or
  • Visual Studio .Net 2005
  • A base class your ASP.NET pages will inherit from
  • Some Visual Basic (VB) code
Show More

Instructions

    • 1
      Base Class for SEO optimization

      Create a base class for your ASP.NET application. All of your WebForms should inherit from this base class. In this example the class is name BasePage.

    • 2
      Change Base Class for all WebForms for SEO Optimization

      In each ASP.Net WebForm change the line that reads Inherits System.Web.UI.Page to Inherits BasePage.

    • 3
      Add a override to the Render Method

      Override the base Render method by adding a subroutine to the BasePage class. This method will do the work of moving the ViewState to the bottom of the page. The function should contain the following code:

      Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
      'Declare a StringWriter object
      Dim stringWriter As System.IO.StringWriter = New System.IO.StringWriter

      'Declare a HtmlTextWriter object
      Dim htmlWriter As HtmlTextWriter = New HtmlTextWriter(stringWriter)

      'Call the default render functionality of ASP.Net to the HtmlWriter object
      MyBase.Render(htmlWriter)

      'Get the page output as a HTML string
      Dim html As String = stringWriter.ToString()

      'Find the position in the HTML string of the start of the ViewState block
      Dim StartPoint As Integer = html.IndexOf("<input type=""hidden"" name=""__VIEWSTATE""")

      'If the ViewState exists, move it
      If StartPoint >= 0 Then 'does __VIEWSTATE exist?

      'Find the postion of the end of the ViewState block
      Dim EndPoint As Integer = html.IndexOf("/>", StartPoint) + 2

      'Put the ViewState block into a string
      Dim ViewStateInput As String = html.Substring(StartPoint, EndPoint - StartPoint)

      'Remove the ViewState block from the page's HTML
      html = html.Remove(StartPoint, EndPoint - StartPoint)

      'Find the end tag for the WebForm
      Dim FormEndStart As Integer = html.IndexOf("</form>")

      'Insert the ViewState block back into the page's HTML just before the end tag
      If FormEndStart >= 0 Then
      html = html.Insert(FormEndStart, ViewStateInput)
      End If
      End If

      'Output the modified HTML
      writer.Write(html)
      End Sub

Tips & Warnings

  • The code in this article applies if you are using Visual Studio 2003 or 2005

Related Searches:

Resources

  • Photo Credit http://www.ieplexus.com/wp-content/uploads/2008/12/seo-blocks.gif

Comments

View all 21 Comments
  • antoinette5000 Feb 26, 2009
    this is so useful. i need to improve my understanding of seo. thanks for sharing!
  • Ruby Kay Feb 22, 2009
    great article.
  • williamskm Feb 20, 2009
    Good tip!!!
  • creativezazz Feb 20, 2009
    cool! Thanks for sharing. 5*
  • katecrittendon Feb 19, 2009
    Good detail! 5*

You May Also Like

Related Ads

Featured