Create new account I forgot my password    

What's New in ASP.NET 4.0 – SEO Enhancements with MetaKeywords and MetaDescription properties
Rating: 4 user(s) have rated this article Average rating: 5.0
Posted by: Suprotim Agarwal, on 3/12/2010, in category "ASP.NET 2.0 & 3.5"
Views: this article has been read 23663 times
Abstract: 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.

What's New in ASP.NET 4.0 – SEO Enhancements with MetaKeywords and MetaDescription properties
 
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 <meta> 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.
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.
If you liked the article,  Subscribe to the RSS Feed or Subscribe Via Email









Page copy protected against web site content infringement by Copyscape


How would you rate this article?

User Feedback
Comment posted by Praveen on Friday, March 12, 2010 3:33 PM
Good addition. However, in a typical web business setup, you'd have your metas read from a repository or an XML file which is maintained by the SEO guy or the biz people. A helper method on your PageBase would usually take care of applying the Metas to the page in question.

Nice post nonetheless!
Comment posted by Welsh on Saturday, March 13, 2010 9:36 AM
Dude I am looking forward to this series. I love your writing. You make it look so simple
Comment posted by Jiteen on Friday, March 26, 2010 2:20 AM
This feature is already present in ASP.NET 3.5. It's not new. Please confirm.
Comment posted by Suprotim Agarwal on Saturday, March 27, 2010 11:44 PM
Jiteen: The article clearly explains how we could do it in ASP.NET 3.5 and how it is done in 4.0 with the addition of new properties
Comment posted by Vishal on Wednesday, March 31, 2010 4:35 AM
The Microsoft .NET Framework 4 coming up with many features for helping developers to create applications faster than ever and fully compatible with the older versions of .NET Framework.

For more details view below URL:

http://blog.conceptinfoway.net/post/2010/03/31/New-Features-in-NET-Framework-40.aspx
Comment posted by ipomz on Monday, April 26, 2010 3:35 AM
thank
Comment posted by Nikunj Dhawan on Monday, May 24, 2010 9:29 AM
Yes... good to know.... can these be data binded?

Post your comment
Name:
E-mail: (Will not be displayed)
Comment:
Insert Cancel

NEWSLETTER