What's New in ASP.NET 4.0 – SEO Enhancements with MetaKeywords and MetaDescription properties

Posted by: Suprotim Agarwal , on 3/12/2010, in Category ASP.NET
Views: 53294
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
Give me a +1 if you think it was a good article. Thanks!
Recommended Articles
Suprotim Agarwal, ASP.NET Architecture MVP, MCSD, MCAD, MCDBA, MCSE, is the CEO of A2Z Knowledge Visuals Pvt. He primarily works as an Architect Consultant and provides consultancy on how to design and develop .NET centric database solutions.

Suprotim is the founder and primary contributor to DotNetCurry, SQLServerCurry and DevCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal


Page copy protected against web site content infringement by Copyscape


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?
Comment posted by single women on Saturday, October 9, 2010 6:43 AM
I cannot WAIT to read more of this. I mean, you just know so much about this. So much of it Ive never even thought of. You sure did put a new twist on something that Ive heard so much about. I dont believe Ive actually read anything that does this subject as good justice as you just did.
Comment posted by Justin on Friday, May 13, 2011 6:25 AM
Hello!!

This was a great post  and very helpful as well.

Thanks
Justin
<a href="http://www.waterdamagelocal.com">Flood Damage</a>
Comment posted by Greg on Tuesday, May 17, 2011 6:56 PM
Useful info, i am actually in the middle of a vb.net project using vb/vs 2010 and silverlight... Helpful! <a href="http://www.floodtechs.com">water damage restoration</a>
Comment posted by Joshone on Monday, March 5, 2012 11:47 PM
Your content is very useful. Thank you so much for providing plenty of useful content. I have bookmarked your site and will be without doubt coming back. Once again, I appreciate all your work and also providing a lot vital tricks for your readers.
Comment posted by SEO Company India on Monday, March 12, 2012 7:06 AM
Excellent site, keep up the good work. I read a lot of blogs on a daily basis and for the most part, people lack substance but, I just wanted to make a quick comment to say I’m glad I found your blog.
<p><a href="http://www.techmagnate.com/seo-company-india.html">SEO Company India</a></p>

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