Meta tags such as description and keywords appear on many web pages and for good reason as it improves SEO (Search Engine Optimization). Adding these meta tags is quite simple, just add the following markup between the <head></head> tag.
<meta name="description" content="description goes here" />
<meta name="keywords" content="k1, k2, k3" />
The question arises how does one insert meta tags into aspx pages when they are contained within a master page? The answer is to use a ContentPlaceHolder within the <head></head> of the master page and then add the specific meta tags to each aspx page. ContentPlaceHolders are common place but are typically used within the <form></form> tag. But there is nothing stopping us from inserting a ContentPlaceHolder elsewhere.
The following master page contains two ContentPlaceHolders; one within the <head></head> tag and the second within the <form></form> tag.
1 <%@ Master Language="C#" AutoEventWireup="true"
2 CodeBehind="Site.master.cs" Inherits="Meta_Tags_in_Master_Page.Site" %>
3
4 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
5 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
6
7 <html xmlns="http://www.w3.org/1999/xhtml" >
8 <head runat="server">
9 <title></title>
10 <asp:ContentPlaceHolder ID="MetaTags" runat="server" />
11 </head>
12 <body>
13 <form id="form1" runat="server">
14 <asp:ContentPlaceHolder ID="cphContent" runat="server" />
15 </form>
16 </body>
17 </html>
The next listing shows the aspx markup that is used as the content page for the master page.
1 <%@ Page Language="C#" MasterPageFile="~/Site.Master"
2 AutoEventWireup="true" CodeBehind="default.aspx.cs"
3 Inherits="Meta_Tags_in_Master_Page._default"
4 Title="Customized Meta Tags" %>
5
6 <asp:Content ID="MetaTags" ContentPlaceHolderID="MetaTags" runat="server">
7 <meta name="description" content="description goes here" />
8 <meta name="keywords" content="k1, k2, k3" />
9 </asp:Content>
10
11 <asp:Content ID="contentContent" ContentPlaceHolderID="cphContent" runat="server" >
12 <h2>Adding customized meta tags using MasterPage and ContentPlaceHolders</h2>
13 </asp:Content>
Within the <asp:Content></asp:Content> tags for the MetaTags ContentPlaceHolder we have essentially two literals; the first for the descriptions meta tag and second for the keywords meta tag. That's all there is to it!
The rendered markup resulting from this aspx page is shown below.
17 <html xmlns="http://www.w3.org/1999/xhtml" >
18 <head>
19 <title>
20 Customized Meta Tags
21 </title>
22 <meta name="description" content="description goes here" />
23 <meta name="keywords" content="k1, k2, k3" />
24 </head>
25 <body>
26 <form name="aspnetForm" method="post" action="default.aspx" id="aspnetForm">
27 <div>
28 <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"
29 value="/wEPDwUKMTY1NDU2MTA1MmRkdMWL8sX25cN6tOeZt9UyAzgvPQQ=" />
30 </div>
31 <h2>Adding customized meta tags using MasterPage and ContentPlaceHolders</h2>
32 </form>
33 </body>
34 </html>