With the ASP.NET 4.0 release round the corner, I thought of starting an article series that covers the new features introduced in ASP.NET 4.0, one at a time. This article is Part I of this multi-part series. In this article, we will take an overview of the two new properties added to the Page class in ASP.NET 4.0 – MetaKeywords and MetaDescription.
The MetaDescription is important from an SEO perspective. The META Description Tag is an integral part which identifies a page and irrespective of the contrary belief, I think search engines take this meta tag seriously. You should too!
Now in ASP.NET 2.0/3.5, you could use the HTMLMeta class to define HTML elements for your page as shown below:
HtmlMeta meta1 = new HtmlMeta();
meta1.Name = "keywords";
meta1.Content = "some keywords";
HtmlMeta meta2 = new HtmlMeta();
meta2.Name = "description";
meta2.Content = "add meta description";
Page.Header.Controls.Add(meta1);
Page.Header.Controls.Add(meta2);
Too much code!
However in ASP.NET 4.0, you now have two new properties added to the Page class – MetaKeywords and MetaDescription.
So you can now declaratively define MetaKeywords and Description directly in your Page as shown below:
<%@ Page Title="New Features in ASP.NET 4" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default"
MetaKeywords="ASP.NET 4, SEO" MetaDescription="This page contains information about ASP.NET 4 and SEO enhancements" %>
The markup gets rendered in the browser as:
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head><title>
New Features in ASP.NET 4
title><link href="Styles/Site.css" rel="stylesheet" type="text/css" />
<meta name="description" content="This page contains information about ASP.NET 4 and SEO enhancements" />
<meta name="keywords" content="ASP.NET 4, SEO" />
head>
...
You can also programmatically define the MetaKeywords and MetaDescription properties by reading them from a database or manually declaring them on the Page object as shown here:
C#
protected void Page_Load(object sender, EventArgs e)
{
Page.MetaDescription = "This page contains information about ASP.NET 4 and SEO enhancements";
Page.MetaKeywords = "ASP.NET 4, SEO";
}
VB.NET
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Page.MetaDescription = "This page contains information about ASP.NET 4 and SEO enhancements"
Page.MetaKeywords = "ASP.NET 4, SEO"
End Sub
Note: If these meta tags are already declared on your page, then they will be overwritten when you follow the techniques shown above. For example, if your page already has existing meta tags..
<head runat="server">
<title>title>
<meta name="description" content="sample content" />
<meta name="keywords" content="k1, k2" />
..and if you explicitly set the MetaDescription and MetaKeywords properties through the Page object, then these properties will overwrite the Meta tag contents already present on the page.
In order to retain the older meta tag contents and also use the new ones, just concatenate the two programmatically as shown below:
C#
protected void Page_Load(object sender, EventArgs e)
{
Page.MetaDescription = Page.MetaDescription + " This page contains information about ASP.NET 4 and SEO enhancements";
Page.MetaKeywords = Page.MetaKeywords + " ASP.NET 4, SEO";
}
VB.NET
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Page.MetaDescription = Page.MetaDescription & " This page contains information about ASP.NET 4 and SEO enhancements"
Page.MetaKeywords = Page.MetaKeywords & " ASP.NET 4, SEO"
End Sub
The Meta tags now get rendered in the browser as shown below, concatenating both the older and new values:
<head><title>
New Features in ASP.NET 4
title>
<meta name="description" content="sample content This page contains information about ASP.NET 4 and SEO enhancements" />
<meta name="keywords" content="k1, k2 ASP.NET 4, SEO" /><link href="Styles/Site.css" rel="stylesheet" type="text/css" />
head>
In the upcoming articles, we will continue exploring new features of ASP.NET 4.0. I hope you liked the article and I thank you for viewing it.