ASP.NET 4 had been released for a few months now so I thought that I’d take a look at two new properties added to the System.Web.Page class – MetaDescription and MetaKeywords. These two attributes represent corresponding meta tags in your page and get rendered on the page like so:
Both the description and keywords meta tags can be important in search engine optimization or (SEO). Specifically, Google (and others) use the description meta tag for improving search listings. (For more details, see Improve snippets with a meta description makeover on the Google Webmaster Central blog.) Also, according to Meta Keywords Advice on the Search Engine Guide Web site, Google no longer uses the contents of the keywords meta tag in their search indexes. It’s my understanding that Windows Live Search doesn’t use the keywords meta tag in determining it’s page rankings, but others may, so it doesn’t hurt to build dynamic content in the keywords meta tag.
These new properties may seem a bit trivial, but prior to ASP.NET 4 if you wanted to provide dynamic values for either of these properties you would have to inject the values into the header. There are two ways of doing this. The first is by adding Literal controls into the element and setting their text in the CodeBehind (Method 1). The other was by creating a HtmlMeta control in the code behind and add them to the control collection for the header element (Method 2).
Here’s an example of Method 1:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="MetaOld.aspx.vb" Inherits="MetaOld" %>
Partial Class MetaOld2
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Page.Title = "your DESCRIPTIVE KEYWORDS title goes here"
Dim metaDescription As New HtmlMeta
Dim metaKeywords As New HtmlMeta
metaDescription.Name = "DESCRIPTION"
metaDescription.Content = "Your keyword rich marketing sales-pitch meta description goes here"
metaKeywords.Name = "KEYWORDS"
metaKeywords.Content = "your keywords,go here,separated by a comma,but not a space"
Since ASP.NET 4, Microsoft introduced the MetaDescription and MetaKeywords attributes in the System.Web.UI.Page class achieving the same behavior is much simpler. You can set these attributes at run time which allows you to generate the content dynamically and provide better descriptions for a particular page (Method 3). Alternatively, you can set the MetaDescription and MetaKeywords attributes declaratively in the @ Page directive at the top of your aspx page (Method 4).
Here’s Method 3, in this example using only the new attributes in the codebehind there isn’t anything needed in the aspx markup to support this.
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="MetaNew.aspx.vb" Inherits="MetaNew" %>
An important note: these properties share a couple of behaviors with the Title attribute of the Page class. First, if there are no description or keywords meta tags in the head element, setting either the MetaDescription or MetaKeywords attributes will cause the meta tags are added to the page when it is rendered. Second, if there are already description or keywords meta tags in the head element, the MetaDescription and MetaKeywords attributes will act as get and set methods for the contents of the existing tags.