.NET life

HUGON Jérôme
Microsoft Certified Technology Specialist Microsoft Certified Application Developer Microsoft Certified Professional

Page.MetaKeywords and Page.MetaDescription Properties

.NET

Category .NET  | Publication Date : 3/28/2010

One of the additions that has been made to ASP.NET 4 is the addition of two properties to the Page class, MetaKeywords and MetaDescription. These two properties represent corresponding meta tags in your page.

The classical way to integrate Meta is the following:

<head id="Head1" runat="server"> 
  <title>My page title</title> 
  <meta name="keywords" content="my, keywords" /> 
  <meta name="description" content="Description of my page" /> 
</head>

Page.MetaKeywords and Page.MetaDescription properties work the same way that the page's Title property does. They follow these rules:

- If there are no meta tags in the head element that match the property names, the meta tags will be added to the page when it is rendered.

- 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.

You can also set the keywords and description properties in the @Page directive at the top of the page markup, as in the following example:

<%@ Page Language="C#" AutoEventWireup="true" 
  CodeFile="Default.aspx.cs" 
  Inherits="_Default" 
  Keywords="my, keywords" 
  Description="Description of my page" %>

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

These new properties are a simple feature, but they save you from the requirement to add these manually or from writing your own code to create the meta tags.