Objects in this mirror are closer to Microsoft Technologies. DNM objective is to help users on Microsoft technologies by providing Articles, snippets, Interview Questions.

07 August 2012

Page.MetaKeywords and Page.MetaDescription feature in ASP.NET 4.0 with Example


The two new properties(Page.MetaDescription and  Page.MetaKeywords) are added to page class in ASP.NET4.0. In this article we will discuss about how to set/get page meta keywords/description data and also we will see in how many ways we can provide the data to meta keywords/description of page and priority of rendering to the client if we specify more than once in the webpage.


Advantage:Meta keywords, description tags  were a great little tools used by search engines to determine the rank of sites. So it’s always better to provide Meta tags information on the page.
  
Page.MetaDescription  – Gets or sets the content of the “discription” Meta element.
Page.MetaKeywords    - Gets or sets the content of the “keywords” Meta element.

Using Page.MetaDescription   and Page.MetaKeywords we can provide the input data from code behind file.The data could be from database or file system.

We can set  meta description/keyword properties in 3 ways
  1. Code behind (Using Page.MetaDescription   and Page.MetaKeywords)
  2. Page directive( using MetaDescription   and MetaKeywords attributes)
  3. Meta tag of head tag.( using meta names)

In the below example we will see how to use above methods in a webpage.

Designer Page
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Page_MetaKeywords _MetaDescription.aspx.cs"
    Inherits="ASPNET.Page_MetaKeywords__MetaDescription" MetaDescription="MetaDescription from Page directive"
    MetaKeywords="MetaKeywords, from, Page, directive" %>

<html>
<head runat='server'>   
    <meta name="keywords" content="keywords,from,head,meta,tag" />
    <meta name="description" content="description of page from head meta tag" />
</head>
<body>
    <form runat="server">
    <div>
        <asp:Button ID="btnGetMetaKeywordsDesc" runat="server"
            Text="Retrieve Page keywords and description"
            onclick="btnGetMetaKeywordsDesc_Click" />
    </div>
    </form>
</body>
</html>
CodeBehind
public partial class Page_MetaKeywords__MetaDescription : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //setting Meta tags data
            Page.MetaKeywords = "MetaKeywords, from, Code, behind";
            Page.MetaDescription = "MetaDescription from Code behind";
        }
        protected void btnGetMetaKeywordsDesc_Click(object sender, EventArgs e)
        {
            //reading Meta tags data
            Response.Write(Page.MetaDescription);
            Response.Write(Page.MetaKeywords);
        }
    }


If we specify the meta data information more than once in a web page, in that case the priority of rendering the meta description/keywords data is
  1. Code behind
  2. Page directive
  3. Meta tags in head tag
Note: If we are using Page.MetaDescription and Page.MetaKeywords we must set runat=”server” for head  tag.otherwise we will get error as
"Using the Description property of Page requires a header control on the page. (e.g. <head runat="server" />)"

0 Comments:

Post a Comment