1/17/11

ASP.NET 4.0 Features - MetaDescription and MetaKeywords

ASP.NET 4.0, came up with two new properties inside Page Class, those are MetaDescription and MetaKeyWord. This has been introduce because of make web application Search Engine Friendly. Search Engine looks for Meta tag of our web page to get the details of page contents. In ASP.NET 4.0, we can add these two properties with page class in Code behind or in Page Directives.


Introduction
ASP.NET 4.0, came up with two new properties inside Page Class, those are MetaDescription and MetaKeyWord. This has been introduce because of make web application Search Engine Friendly. Search Engine looks for Meta tag of our web page to get the details of page contents. In ASP.NET 4.0, we can add these two properties with page class in Code behind or in Page Directives.

If you want to find out the definition of these two properties, Right Click on Page Class and Click on Goto Definition. This will show you the Meta data information of Page Class as shown in below picture.


Fig:Page Class with MetaDescription and MetaKeyWords Properies
How to use ?


If we set MetaDescription and MetaKeywords either from Code behind or using Page Directive in aspx page, both will be render as “meta” tag in html code.
Let have a look, how we can set these two properties from code behind,

protected void Page_Load(object sender, EventArgs e)

{

Page.MetaKeywords = "ASP.NET 4.0, .NET 4.0";

Page.MetaDescription = "ASP.NET 4.0 Information";

}



Fig: Set MetaKeywords and MetaDescription Properties using Codebehind

Now, If I run the application and check the HTML rendered content we will found the following code,

<head>

<meta content="ASP.NET 4.0 Information" name="description" />

<meta content="ASP.NET 4.0, .NET 4.0" name="keywords" />

head>

Similarly we can also add MetaKeywords and MetaDescription Properties in Page Directive Itself.


But the HTML output will be same for both the case.

Fig: HTML Rendered content of MetaKeywords and MetaDescription



Setting Meta Tags with the Page.MetaKeywords and Page.MetaDescription Properties

ASP.NET 4 adds two properties to the Page class, MetaKeywords and MetaDescription. These two properties represent corresponding meta tags in your page, as shown in the following example:



Untitled Page



These two properties work the same way that the page’s Title property does. They follow these rules:

  1. If there are no meta tags in the head element that match the property names (that is, name="keywords" for Page.MetaKeywords and name="description" for Page.MetaDescription, meaning that these properties have not been set), the meta tags will be added to the page when it is rendered.
  2. If there are already meta tags with these names, these properties act as get and set methods for the contents of the existing tags.

You can set these properties at run time, which lets you get the content from a database or other source, and which lets you set the tags dynamically to describe what a particular page is for.

You can also set the Keywords and Description properties in the @ Page directive at the top of the Web Forms page markup, as in the following example:

<%@ Page Language="C#" AutoEventWireup="true"

CodeFile="Default.aspx.cs"
Inherits="_Default"
Keywords="These, are, my, keywords"
Description="This is a description" %>

This will override the meta tag contents (if any) already declared in the page.

Conclusion
The main objective of MetaKeywords and MetaDescription proerties to make your web application SEO friendly. In ASP.NET 2.0, HtmlMeta used to do the same, but in ASP.NET 4.0 make these thing very simple as we can easily add using Page Class.

Welcome