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.
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.
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!
Was this article worth reading? Share it with fellow developers too. Thanks!
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of
DotNetCurry,
DNC Magazine for Developers,
SQLServerCurry and
DevCurry. He has also authored a couple of books
51 Recipes using jQuery with ASP.NET Controls and
The Absolutely Awesome jQuery CookBook.
Suprotim has received the prestigious Microsoft MVP award for Sixteen consecutive years. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.
Get in touch with him on Twitter @suprotimagarwal or at LinkedIn