Tuesday, August 2, 2011

ASP.Net 4.0 Head Tag and Elements New Features

ASP.Net 4.0 comes with the first major overhaul of the .Net Framework since 2.0 (The 3.x version were simply add-ons to 2.0).  With that comes a new host of options on how you can render your pages.  Here we are going to look at the Head element of your page.
Although Search Engines (Such as Google) have somewhat deprecated the Meta Keywords and Meta Description tags that are used to describe the content of your page due to abuse, they are still very important in the algorithm that they use to index the page.  Google’s SEO works like this:
·         If a keyword appears more than once in your keyword list, it is removed from the algorithm as it is considered “overloading” of a keyword.
·         Keyword lists should be somewhere between the 25 word or less range.
·         The more often a word appears in your page the higher rank (for your page) the word will be considered in the algorithm.
·         Words that are in your keyword list and also appear in: The pages title, headers, sections, and names of objects (like tables) will also be of higher rank.
That being said, Google has also put logic into its algorithm that looks for word “overloading” within the page itself.  This is also due to abuse, where a developer would hide words listed at the bottom of pages so that it would appear to the crawler (or spider) that the word is used in context more often and thus more relevant to your page.  The thing to remember is that Google’s bread and butter is Search Engine.  Of course there will always be developers who will find some new hack or work around to try and circumvent the algorithm and get a higher listing.  So the rules for its algorithms are always changing and a very close guarded secret (Coke won’t be receiving their crawler ingredient list anytime soon).

The <head> Tag:
The Meta Keywords and Meta Description elements have been added as properties of the Page itself.  This opens many doors that were previously closed to developers.
In ASP.Net 4.0 there are now 3 ways you can add these two elements to your header – Inside the Page Declaration, On the Page UI (using HTML), and declaratively in code-behind.  Let’s look at each method individually.

Page Declaration:
Arguably the most dirty way to add these elements to the page is in the declaration.  This crowds the declaration and makes it harder to read.  However, the option is open to the developer.
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" 
Inherits="_Default" MetaDescription="My Test Website" 
MetaKeywords="Test, Development, Keywords" %>

As you can see, in the Page Declaration we have added the MetaDescription and MetaKeywords tags.  You can imagine fairly easily how long this Page Declaration could get if we put all our keywords and the description here.  A distinct advantage for using the Page Declaration is that you can have different descriptions and keywords per page, independent of the Master Page.  If you have a Master Page and then Content Pages with these tags in their declaration, the Content Pages win out as with the Title property. 
Due to the MetaDescription and MetaKeyword being Properties of the Page, if while you are working on the Page, you open the Proeprties Pane you can see them listed there and can populate them inside the Pane.  This will then add it to the Page Declaration upon losing focus.

Declaratively In Code-Behind:
You can also add the tags through Code-Behind.  Here you will need a trigger (as in Page Load) to add a value to the Page Properties.
Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs)
 Handles Me.Load
        Page.MetaDescription = "This is a description of my Page."
        Page.MetaKeywords = "programming, examples, testing"
        'Or using the ME object
        Me.Page.MetaDescription = "This is a description of my Page."
        Me.Page.MetaKeywords = "programming, examples, testing"
    End Sub

This gives us the option to programmatically change the keywords and description based on conditions.  It also gives us the ability to have a data store of the properties and then serve them to the page.

You can always do the tried and true method and simply put your Meta Tags in the <head> declarations of your HTML page.  It will simply read as it always has in a single linear fashion.

One additional feature that is somewhat relevant is the Permanent Redirection routing.  In the past you would have to change the HTTP Status of the Header manually.  In ASP.Net 4.0 MS has provided a function for you:
Response.RedirectPermanent(“http://www.RedirectedPage.com “)

What this does is tell spiders like Google and Yahoo to update their index of your site.  It automatically responds with a HTTP 301 – Permenant Redirection.  Google will then re-index your page and update its data store.

Happy .Netting… Saleh

No comments:

Post a Comment