Thursday, January 3, 2013

Using Meta Tags with Master Pages in ASP.NET

By , 24 May 2007

Download source code (VB) - 3 Kb

Introduction

The Master Pages introduced in ASP.NET 2.0 are a great feature, however, they don't provide a good way to perform the most basic search engine optimization. If you want your web pages to be listed properly and ranked well in search engines, then you need to specify good titles and meta tag descriptions on each page. This article explains how to extend the @Page directive on your ASP.NET pages so that you can specify the meta tag description and meta tag keywords on each content page when using master pages.

Background

When optimizing your web pages for search engines, some of the most important elements on the page are the tag and the description meta tag. The <title> and meta <span class="search-highlight">tags</span> are usually specified in the <head> section of the HTML on each page as seen in the example below from the <a href="http://www.rhinoback.com/" target="_blank">Rhinoback online backup site</a>: </p> <div class="pre-action-link" id="premain0" style="display: block;" width="100%"> <img height="9" id="preimg0" preid="0" src="http://www.codeproject.com/images/minus.gif" style="cursor: pointer;" width="9" /><span id="precollapse0" preid="0" style="cursor: pointer; margin-bottom: 0px;"> Collapse</span><span> | </span><a href="http://www.codeproject.com/Articles/17368/Using-Meta-Tags-with-Master-Pages-in-ASP-NET#" preid="0">Copy Code</a></div> <div class="article-text"> <pre id="pre0" lang="html" style="margin-top: 0px;"><span class="code-keyword"><</span><span class="code-leadattribute">html</span> <span class="code-attribute">xmlns</span><span class="code-keyword">="</span><span class="code-keyword">http://www.w3.org/1999/xhtml"</span> <span class="code-keyword">></span> <span class="code-keyword"><</span><span class="code-leadattribute">head</span><span class="code-keyword">></span> <span class="code-keyword"><</span><span class="code-leadattribute">title</span><span class="code-keyword">></span> Rhinoback Professional Secure Online Backup Services for Small and Medium Business - SMB <span class="code-keyword"><</span><span class="code-keyword">/</span><span class="code-leadattribute">title</span><span class="code-keyword">></span> <span class="code-keyword"><</span><span class="code-leadattribute">meta</span> <span class="code-attribute">name</span><span class="code-keyword">="</span><span class="code-keyword">description"</span> <span class="code-attribute">content</span><span class="code-keyword">="</span><span class="code-keyword">Professional Online Backup Services. Rhinoback provides robust backup functionality at affordable prices. Premium features, premium services, low prices. Get the most for your money with Rhinoback!"</span> <span class="code-keyword">/</span><span class="code-keyword">></span> <span class="code-keyword"><</span><span class="code-leadattribute">meta</span> <span class="code-attribute">name</span><span class="code-keyword">="</span><span class="code-keyword">keywords"</span> <span class="code-attribute">content</span><span class="code-keyword">="</span><span class="code-keyword">backup, online backup, secure backup, cheap backup, free backup, offsite backup,internet backup, secure files, offsite data storage, privacy, security, features, low prices, premium service, remote backup"</span> <span class="code-keyword">/</span><span class="code-keyword">></span> <span class="code-keyword"><</span><span class="code-keyword">/</span><span class="code-leadattribute">head</span><span class="code-keyword">></span> <span class="code-keyword"><</span><span class="code-leadattribute">body</span><span class="code-keyword">></span> <span class="code-keyword"><!--</span><span class="code-keyword"><span class="code-comment"> page content </span>--></span> <span class="code-keyword"><</span><span class="code-keyword">/</span><span class="code-leadattribute">body</span><span class="code-keyword">></span> <span class="code-keyword"><</span><span class="code-keyword">/</span><span class="code-leadattribute">html</span><span class="code-keyword">></span> </pre> </div> <p class="article-text"> The text from the <title> tag is displayed at the top of most browsers. See the title of the Internet Explorer window in the example below:</p> <p class="article-text"> <a href="http://www.rhinoback.com/" target="_blank"><img alt="Sample of Rhinoback page with title tag" border="0" height="216" src="http://www.codeproject.com/KB/aspnet/PageTags/RhinobackPage.jpg" width="593" /> </a><br /></p> <p class="article-text"> The meta description text is displayed by the search engine when your page is listed. The example below is from a Google search. The text below the underlined title comes directly from the meta description tag. Without a meta description tag, your page may be listed with a description that was extracted from text found somewhere on the page. It is always better to specify the text for the description of each page rather than leave it up to a search engine robot.</p> <p class="article-text"> <a href="http://www.rhinoback.com/" target="_blank"><img border="0" height="91" lt="Sample of Rhinoback listing in Google" src="http://www.codeproject.com/KB/aspnet/PageTags/RhinobackGoogle.jpg" width="592" /> </a><br /></p> <p class="article-text"> Master Pages were introduced in <span class="search-highlight">ASP.NET</span> 2.0 and have proven to be a valuable feature. This article does not attempt to explain the details of master pages or how to implement them, that is well covered in numerous other articles. When using master pages the <head> section is part of the master page and is automatically included on all of the content pages. Fortunately the developers at Microsoft included the <strong>Title</strong> attribute on the @Page directive that enables the developer to specify the title of the page on the content pages rather than on the master page.</p> <div class="pre-action-link" id="premain1" style="display: block;" width="100%"> <img height="9" id="preimg1" preid="1" src="http://www.codeproject.com/images/minus.gif" style="cursor: pointer;" width="9" /><span id="precollapse1" preid="1" style="cursor: pointer; margin-bottom: 0px;"> Collapse</span><span> | </span><a href="http://www.codeproject.com/Articles/17368/Using-Meta-Tags-with-Master-Pages-in-ASP-NET#" preid="1">Copy Code</a></div> <div class="article-text"> <pre id="pre1" lang="html" style="margin-top: 0px;"><span class="code-pagedirective"><%@</span><span class="code-leadattribute"> Page Language</span><span class="code-keyword">="</span><span class="code-keyword">C#"</span><span class="code-attribute"> MasterPageFile</span><span class="code-keyword">="</span><span class="code-keyword">~/Page<span class="search-highlight">Tags</span>.master"</span><span class="code-attribute"> AutoEventWireup</span><span class="code-keyword">="</span><span class="code-keyword">true"</span><span class="code-attribute"> CodeFile</span><span class="code-keyword">="</span><span class="code-keyword">home.aspx.cs"</span><span class="code-attribute"> Inherits</span><span class="code-keyword">="</span><span class="code-keyword">home"</span><span class="code-attribute"> Title</span><span class="code-keyword">="</span><span class="code-keyword">My home page title"</span><span class="code-attribute"> </span><span class="code-pagedirective">%></span> </pre> </div> <p class="article-text"> The @Page directive above is from a content page in an <span class="search-highlight">ASP.NET</span> 2.0 website that uses a master page. As discussed above, you may need to specify some meta <span class="search-highlight">tags</span> at the content page level rather than at the master page level. You may have discovered that the @Page directive does have a <strong>Description</strong> attribute, however, it does not create a meta description tag for your page. In fact, anything that you specify on the <strong>Description</strong> attribute is completely ignored and not used for anything.</p> <p class="article-text"> In my case, it was completely unacceptable to have the same description on all pages of my site. I also wanted to specify keywords for each page that may vary from page to page. My first cut at a solution to this problem involved using the code-behind to insert the desired meta <span class="search-highlight">tags</span> onto each pages' <head> section as shown in the Page_Load method of a content page below: </p> <h3 class="article-text"> C#</h3> <div class="pre-action-link" id="premain2" style="display: block;" width="100%"> <img height="9" id="preimg2" preid="2" src="http://www.codeproject.com/images/minus.gif" style="cursor: pointer;" width="9" /><span id="precollapse2" preid="2" style="cursor: pointer; margin-bottom: 0px;"> Collapse</span><span> | </span><a href="http://www.codeproject.com/Articles/17368/Using-Meta-Tags-with-Master-Pages-in-ASP-NET#" preid="2">Copy Code</a></div> <div class="article-text"> <pre id="pre2" lang="cs" style="margin-top: 0px;"> <span class="code-keyword">protected</span> <span class="code-keyword">void</span> Page_Load(<span class="code-keyword">object</span> sender, EventArgs e) { HtmlMeta tag = <span class="code-keyword">new</span> HtmlMeta(); tag.Name = <span class="code-string">"</span><span class="code-string">description"</span>; tag.Content = <span class="code-string">"</span><span class="code-string">My description for this page"</span>; Header.Controls.Add(tag); } </pre> </div> <h3 class="article-text"> VB</h3> <div class="pre-action-link" id="premain3" style="display: block;" width="100%"> <img height="9" id="preimg3" preid="3" src="http://www.codeproject.com/images/minus.gif" style="cursor: pointer;" width="9" /><span id="precollapse3" preid="3" style="cursor: pointer; margin-bottom: 0px;"> Collapse</span><span> | </span><a href="http://www.codeproject.com/Articles/17368/Using-Meta-Tags-with-Master-Pages-in-ASP-NET#" preid="3">Copy Code</a></div> <div class="article-text"> <pre id="pre3" lang="vb" style="margin-top: 0px;"> <span class="code-keyword">Sub</span> Page_Load(<span class="code-keyword">ByVal</span> sender <span class="code-keyword">As</span> <span class="code-keyword">Object</span>, <span class="code-keyword">ByVal</span> e <span class="code-keyword">As</span> EventArgs) <span class="code-keyword">Dim</span> tag <span class="code-keyword">As</span> HtmlMeta = <span class="code-keyword">New</span> HtmlMeta() tag.Name = <span class="code-string">"</span><span class="code-string">description"</span> tag.Content = <span class="code-string">"</span><span class="code-string">My description for this page"</span> Header.Controls.Add(tag) <span class="code-keyword">End</span> <span class="code-keyword">Sub</span> </pre> </div> <p class="article-text"> The problem with this solution is that the title of the page, the meta description, and the content of a page are all related and I really want the title and description to be together and also in the same ASPX file as the content. The <code>Page_Load</code> method can easily be placed within <script> <span class="search-highlight">tags</span> on the ASPX page, but I wanted to find a solution that was easier to maintain and would allow for quick and easy inspection of the <span class="search-highlight">tags</span> on each page. </p> <p class="article-text"> The following solution meets my objectives by extending the @Page directive to include the meta <span class="search-highlight">tags</span> that I want to specify for each page individually. </p> <h2 class="article-text"> Solution</h2> <p class="article-text"> I created a base page class that inherits from <code>System.Web.UI.Page</code> and then I modified my content pages to inherit from my <code>BasePage</code> class. The <code>BasePage</code> class contains the code that adds the meta <span class="search-highlight">tags</span> to the header controls collection on the page. Since all of my content pages are inheriting from <code>BasePage</code>, this code only needs to exist in one place and not on every page. </p> <h3 class="article-text"> C#</h3> <div class="pre-action-link" id="premain4" style="display: block;" width="100%"> <img height="9" id="preimg4" preid="4" src="http://www.codeproject.com/images/minus.gif" style="cursor: pointer;" width="9" /><span id="precollapse4" preid="4" style="cursor: pointer; margin-bottom: 0px;"> Collapse</span><span> | </span><a href="http://www.codeproject.com/Articles/17368/Using-Meta-Tags-with-Master-Pages-in-ASP-NET#" preid="4">Copy Code</a></div> <div class="article-text"> <pre id="pre4" lang="cs" style="margin-top: 0px;"><span class="code-comment">/*</span><span class="code-comment"> ********************************************** * Page directive extender - base page class * * by Jim Azar - http://www.rhinoback.com * * **********************************************/</span> <span class="code-keyword">using</span> System; <span class="code-keyword">using</span> System.Web.UI; <span class="code-keyword">using</span> System.Web.UI.HtmlControls; <span class="code-keyword">using</span> System.Text.RegularExpressions; <span class="code-SummaryComment">///</span><span class="code-comment"> <span class="code-SummaryComment"><</span><span class="code-SummaryComment">SUMMARY</span><span class="code-SummaryComment">></span> </span><span class="code-SummaryComment">///</span><span class="code-comment"> Base class with properties for meta <span class="search-highlight">tags</span> for content pages </span><span class="code-SummaryComment">///</span><span class="code-comment"> <span class="code-SummaryComment"><</span><span class="code-SummaryComment">/</span><span class="code-SummaryComment">SUMMARY</span><span class="code-SummaryComment">></span> </span><span class="code-keyword">public</span> <span class="code-keyword">class</span> BasePage : Page { <span class="code-keyword">private</span> <span class="code-keyword">string</span> _keywords; <span class="code-keyword">private</span> <span class="code-keyword">string</span> _description; <span class="code-comment">//</span><span class="code-comment"> Constructor </span> <span class="code-comment">//</span><span class="code-comment"> Add an event handler to Init event for the control </span> <span class="code-comment">//</span><span class="code-comment"> so we can execute code when a server control (page) </span> <span class="code-comment">//</span><span class="code-comment"> that inherits from this base class is initialized. </span> <span class="code-keyword">public</span> BasePage() { Init += <span class="code-keyword">new</span> EventHandler(BasePage_Init); } <span class="code-comment">//</span><span class="code-comment"> Whenever a page that uses this base class is initialized </span> <span class="code-comment">//</span><span class="code-comment"> add meta keywords and descriptions if available </span> <span class="code-keyword">void</span> BasePage_Init(<span class="code-keyword">object</span> sender, EventArgs e) { <span class="code-keyword">if</span> (!String.IsNullOrEmpty(Meta_Keywords)) { HtmlMeta tag = <span class="code-keyword">new</span> HtmlMeta(); tag.Name = <span class="code-string">"</span><span class="code-string">keywords"</span>; tag.Content = Meta_Keywords; Header.Controls.Add(tag); } <span class="code-keyword">if</span> (!String.IsNullOrEmpty(Meta_Description)) { HtmlMeta tag = <span class="code-keyword">new</span> HtmlMeta(); tag.Name = <span class="code-string">"</span><span class="code-string">description"</span>; tag.Content = Meta_Description; Header.Controls.Add(tag); } } <span class="code-SummaryComment">///</span><span class="code-comment"> <span class="code-SummaryComment"><</span><span class="code-SummaryComment">SUMMARY</span><span class="code-SummaryComment">></span> </span> <span class="code-SummaryComment">///</span><span class="code-comment"> Gets or sets the Meta Keywords tag for the page </span> <span class="code-SummaryComment">///</span><span class="code-comment"> <span class="code-SummaryComment"><</span><span class="code-SummaryComment">/</span><span class="code-SummaryComment">SUMMARY</span><span class="code-SummaryComment">></span> </span> <span class="code-keyword">public</span> <span class="code-keyword">string</span> Meta_Keywords { <span class="code-keyword">get</span> { <span class="code-keyword">return</span> _keywords; } <span class="code-keyword">set</span> { <span class="code-comment">//</span><span class="code-comment"> strip out any excessive white-space, newlines and linefeeds </span> _keywords = Regex.Replace(value, <span class="code-string">"</span><span class="code-string">\\s+"</span>, <span class="code-string">"</span><span class="code-string"> "</span>); } } <span class="code-SummaryComment">///</span><span class="code-comment"> <span class="code-SummaryComment"><</span><span class="code-SummaryComment">SUMMARY</span><span class="code-SummaryComment">></span> </span> <span class="code-SummaryComment">///</span><span class="code-comment"> Gets or sets the Meta Description tag for the page </span> <span class="code-SummaryComment">///</span><span class="code-comment"> <span class="code-SummaryComment"><</span><span class="code-SummaryComment">/</span><span class="code-SummaryComment">SUMMARY</span><span class="code-SummaryComment">></span> </span> <span class="code-keyword">public</span> <span class="code-keyword">string</span> Meta_Description { <span class="code-keyword">get</span> { <span class="code-keyword">return</span> _description; } <span class="code-keyword">set</span> { <span class="code-comment">//</span><span class="code-comment"> strip out any excessive white-space, newlines and linefeeds </span> _description = Regex.Replace(value, <span class="code-string">"</span><span class="code-string">\\s+"</span>, <span class="code-string">"</span><span class="code-string"> "</span>); } } }</pre> </div> <h3 class="article-text"> VB</h3> <div class="pre-action-link" id="premain5" style="display: block;" width="100%"> <img height="9" id="preimg5" preid="5" src="http://www.codeproject.com/images/minus.gif" style="cursor: pointer;" width="9" /><span id="precollapse5" preid="5" style="cursor: pointer; margin-bottom: 0px;"> Collapse</span><span> | </span><a href="http://www.codeproject.com/Articles/17368/Using-Meta-Tags-with-Master-Pages-in-ASP-NET#" preid="5">Copy Code</a></div> <div class="article-text"> <pre id="pre5" lang="vb" style="margin-top: 0px;"><span class="code-comment">'</span><span class="code-comment">* ********************************************** </span><span class="code-comment">'</span><span class="code-comment">* Page directive extender - base page class * </span><span class="code-comment">'</span><span class="code-comment">* by Jim Azar - http://www.rhinoback.com * </span><span class="code-comment">'</span><span class="code-comment">* **********************************************/ </span> <span class="code-keyword">Imports</span> System <span class="code-keyword">Imports</span> System.Web.UI <span class="code-keyword">Imports</span> System.Web.UI.HtmlControls <span class="code-keyword">Imports</span> System.Text.RegularExpressions <span class="code-comment">'</span><span class="code-comment"> Base class with properties for meta <span class="search-highlight">tags</span> for content pages </span> <span class="code-keyword">Public</span> <span class="code-keyword">Class</span> BasePage <span class="code-keyword">Inherits</span> Page <span class="code-keyword">Dim</span> _keywords <span class="code-keyword">As</span> <span class="code-keyword">String</span> <span class="code-keyword">Dim</span> _description <span class="code-keyword">As</span> <span class="code-keyword">String</span> <span class="code-comment">'</span><span class="code-comment"> Constructor </span> <span class="code-comment">'</span><span class="code-comment"> Add an event handler to Init event for the control </span> <span class="code-comment">'</span><span class="code-comment"> so we can execute code when a server control (page) </span> <span class="code-comment">'</span><span class="code-comment"> that inherits from this base class is initialized. </span> <span class="code-keyword">Public</span> <span class="code-keyword">Sub</span> <span class="code-keyword">New</span>() <span class="code-keyword">AddHandler</span> Init, <span class="code-keyword">New</span> EventHandler(<span class="code-keyword">AddressOf</span> BasePage_Init) <span class="code-keyword">End</span> <span class="code-keyword">Sub</span> <span class="code-comment">'</span><span class="code-comment"> Whenever a page that uses this base class is initialized </span> <span class="code-comment">'</span><span class="code-comment"> add meta keywords and descriptions if available </span> <span class="code-keyword">Sub</span> BasePage_Init(<span class="code-keyword">ByVal</span> sender <span class="code-keyword">As</span> <span class="code-keyword">Object</span>, <span class="code-keyword">ByVal</span> e <span class="code-keyword">As</span> EventArgs) <span class="code-keyword">If</span> <span class="code-keyword">Not</span> <span class="code-keyword">String</span>.IsNullOrEmpty(Meta_Keywords) <span class="code-keyword">Then</span> <span class="code-keyword">Dim</span> tag <span class="code-keyword">As</span> HtmlMeta = <span class="code-keyword">New</span> HtmlMeta() tag.Name = <span class="code-string">"</span><span class="code-string">keywords"</span> tag.Content = Meta_Keywords Header.Controls.Add(tag) <span class="code-keyword">End</span> <span class="code-keyword">If</span> <span class="code-keyword">If</span> <span class="code-keyword">Not</span> <span class="code-keyword">String</span>.IsNullOrEmpty(Meta_Description) <span class="code-keyword">Then</span> <span class="code-keyword">Dim</span> tag <span class="code-keyword">As</span> HtmlMeta = <span class="code-keyword">New</span> HtmlMeta() tag.Name = <span class="code-string">"</span><span class="code-string">description"</span> tag.Content = Meta_Description Header.Controls.Add(tag) <span class="code-keyword">End</span> <span class="code-keyword">If</span> <span class="code-keyword">End</span> <span class="code-keyword">Sub</span> <span class="code-comment">'</span><span class="code-comment">Gets or sets the Meta Keywords tag for the page </span> <span class="code-keyword">Public</span> <span class="code-keyword">Property</span> Meta_Keywords() <span class="code-keyword">As</span> <span class="code-keyword">String</span> <span class="code-keyword">Get</span> <span class="code-keyword">Return</span> _keywords <span class="code-keyword">End</span> <span class="code-keyword">Get</span> <span class="code-keyword">set</span> <span class="code-comment">'</span><span class="code-comment"> strip out any excessive white-space, newlines and linefeeds </span> _keywords = Regex.Replace(value, <span class="code-string">"</span><span class="code-string">\\s+"</span>, <span class="code-string">"</span><span class="code-string"> "</span>) <span class="code-keyword">End</span> <span class="code-keyword">Set</span> <span class="code-keyword">End</span> <span class="code-keyword">Property</span> <span class="code-comment">'</span><span class="code-comment"> Gets or sets the Meta Description tag for the page </span> <span class="code-keyword">Public</span> <span class="code-keyword">Property</span> Meta_Description() <span class="code-keyword">As</span> <span class="code-keyword">String</span> <span class="code-keyword">Get</span> <span class="code-keyword">Return</span> _description <span class="code-keyword">End</span> <span class="code-keyword">Get</span> <span class="code-keyword">Set</span>(<span class="code-keyword">ByVal</span> value <span class="code-keyword">As</span> <span class="code-keyword">String</span>) <span class="code-comment">'</span><span class="code-comment"> strip out any excessive white-space, newlines and linefeeds </span> _description = Regex.Replace(value, <span class="code-string">"</span><span class="code-string">\\s+"</span>, <span class="code-string">"</span><span class="code-string"> "</span>) <span class="code-keyword">End</span> <span class="code-keyword">Set</span> <span class="code-keyword">End</span> <span class="code-keyword">Property</span> <span class="code-keyword">End</span> <span class="code-keyword">Class</span></pre> </div> <p class="article-text"> The <code>Meta_Keywords</code> and <code>Meta_Description</code> properties are public and can be set when the class (or derived class) is instantiated. When a page that inherits from this class is initialized, the <code>Base_Init</code> event handler is invoked and adds the meta <span class="search-highlight">tags</span> to the page.</p> <p class="article-text"> On each content page, simply change the inheritance so that they inherit from <code>BasePage</code> instead of <code>page</code> or <code>System.Web.UI.Page</code>. See below: </p> <h3 class="article-text"> C#</h3> <div class="pre-action-link" id="premain6" style="display: block;" width="100%"> <img height="9" id="preimg6" preid="6" src="http://www.codeproject.com/images/minus.gif" style="cursor: pointer;" width="9" /><span id="precollapse6" preid="6" style="cursor: pointer; margin-bottom: 0px;"> Collapse</span><span> | </span><a href="http://www.codeproject.com/Articles/17368/Using-Meta-Tags-with-Master-Pages-in-ASP-NET#" preid="6">Copy Code</a></div> <div class="article-text"> <pre id="pre6" lang="cs" style="margin-top: 0px;"><span class="code-keyword">public</span> <span class="code-keyword">partial</span> <span class="code-keyword">class</span> home : BasePage { <span class="code-keyword">protected</span> <span class="code-keyword">void</span> Page_Load(<span class="code-keyword">object</span> sender, EventArgs e) { } }</pre> </div> <h3 class="article-text"> VB</h3> <div class="pre-action-link" id="premain7" style="display: block;" width="100%"> <img height="9" id="preimg7" preid="7" src="http://www.codeproject.com/images/minus.gif" style="cursor: pointer;" width="9" /><span id="precollapse7" preid="7" style="cursor: pointer; margin-bottom: 0px;"> Collapse</span><span> | </span><a href="http://www.codeproject.com/Articles/17368/Using-Meta-Tags-with-Master-Pages-in-ASP-NET#" preid="7">Copy Code</a></div> <div class="article-text"> <pre id="pre7" lang="vb" style="margin-top: 0px;"><span class="code-keyword">Partial</span> <span class="code-keyword">Class</span> home <span class="code-keyword">Inherits</span> BasePage <span class="code-keyword">Sub</span> Page_Load(<span class="code-keyword">ByVal</span> sender <span class="code-keyword">As</span> <span class="code-keyword">Object</span>, <span class="code-keyword">ByVal</span> e <span class="code-keyword">As</span> EventArgs) <span class="code-keyword">End</span> <span class="code-keyword">Sub</span> <span class="code-keyword">End</span> <span class="code-keyword">Class</span></pre> </div> <p class="article-text"> Now that each content page is inheriting from <code>BasePage</code>, they have the properties and the code to insert the meta <span class="search-highlight">tags</span>. Now we can specify the <code>Meta_Keywords</code> and <code>Meta_Description</code> values on the @Page directive on the ASPX file. See the examples below: </p> <div class="pre-action-link" id="premain8" style="display: block;" width="100%"> <img height="9" id="preimg8" preid="8" src="http://www.codeproject.com/images/minus.gif" style="cursor: pointer;" width="9" /><span id="precollapse8" preid="8" style="cursor: pointer; margin-bottom: 0px;"> Collapse</span><span> | </span><a href="http://www.codeproject.com/Articles/17368/Using-Meta-Tags-with-Master-Pages-in-ASP-NET#" preid="8">Copy Code</a></div> <div class="article-text"> <pre id="pre8" lang="html" style="margin-top: 0px;"><span class="code-pagedirective"><%@</span><span class="code-leadattribute"> Page Language</span><span class="code-keyword">="</span><span class="code-keyword">C#"</span><span class="code-attribute"> MasterPageFile</span><span class="code-keyword">="</span><span class="code-keyword">~/Page<span class="search-highlight">Tags</span>.master"</span><span class="code-attribute"> AutoEventWireup</span><span class="code-keyword">="</span><span class="code-keyword">true"</span><span class="code-attribute"> CodeFile</span><span class="code-keyword">="</span><span class="code-keyword">home.aspx.cs"</span><span class="code-attribute"> Inherits</span><span class="code-keyword">="</span><span class="code-keyword">home"</span><span class="code-attribute"> CodeFileBaseClass</span><span class="code-keyword">="</span><span class="code-keyword">BasePage"</span><span class="code-attribute"> Title</span><span class="code-keyword">="</span><span class="code-keyword">My home page title"</span><span class="code-attribute"> Meta_Keywords</span><span class="code-keyword">="</span><span class="code-keyword">page directive, extension, dotnet, <span class="search-highlight">asp.net</span>"</span><span class="code-attribute"> Meta_Description</span><span class="code-keyword">="</span><span class="code-keyword">This is the meta description for my home page."</span><span class="code-attribute"> </span><span class="code-pagedirective">%></span> <span class="code-keyword"><</span><span class="code-leadattribute">asp:Content</span> <span class="code-attribute">ID</span><span class="code-keyword">="</span><span class="code-keyword">Content1"</span> <span class="code-attribute">ContentPlaceHolderID</span><span class="code-keyword">="</span><span class="code-keyword">ContentPlaceHolder1"</span> <span class="code-attribute">Runat</span><span class="code-keyword">="</span><span class="code-keyword">Server"</span><span class="code-keyword">></span> <span class="code-keyword"><</span><span class="code-leadattribute">h3</span><span class="code-keyword">></span>My home page content<span class="code-keyword"><</span><span class="code-leadattribute">h3</span><span class="code-keyword">></span> <span class="code-keyword"><</span><span class="code-leadattribute">p</span><span class="code-keyword">></span> This is the content on my home page. This page has an appropriate title tag and also has meta <span class="search-highlight">tags</span> for keywords and description that are relative to this page. The title tag is essential to good search engine optimization and the meta description is the text that the search engine will display when your page is listed in search results. The title and meta description should be set specific to each page and should describe the content of the page. <span class="code-keyword"><</span><span class="code-keyword">/</span><span class="code-leadattribute">p</span><span class="code-keyword">></span> <span class="code-keyword"><</span><span class="code-keyword">/</span><span class="code-leadattribute">asp:Content</span><span class="code-keyword">></span> </pre> </div> <p class="article-text"> Note the addition of the <code>CodeFileBaseClass</code> attribute. This is required so that page can reference the public properties specified in the <code>BasePage</code> class.</p> <h2 class="article-text"> Points of Interest</h2> <p class="article-text"> You may have noticed the regular expression in the <code>BasePage</code> class. This is here so that you can break the description and keyword <span class="search-highlight">tags</span> up onto mulitple lines in your ASPX file, making them more readable and maintainable. Consider the following example from the <a href="http://www.ideascope.com/" target="_blank">IdeaScope Customer Feedback Management</a> website:</p> <div class="pre-action-link" id="premain9" style="display: block;" width="100%"> <img height="9" id="preimg9" preid="9" src="http://www.codeproject.com/images/minus.gif" style="cursor: pointer;" width="9" /><span id="precollapse9" preid="9" style="cursor: pointer; margin-bottom: 0px;"> Collapse</span><span> | </span><a href="http://www.codeproject.com/Articles/17368/Using-Meta-Tags-with-Master-Pages-in-ASP-NET#" preid="9">Copy Code</a></div> <div class="article-text"> <pre id="pre9" lang="html" style="margin-top: 0px;"><span class="code-pagedirective"><%@</span><span class="code-leadattribute"> Page Language</span><span class="code-keyword">="</span><span class="code-keyword">C#"</span><span class="code-attribute"> MasterPageFile</span><span class="code-keyword">="</span><span class="code-keyword">~/IdeaScope.master"</span><span class="code-attribute"> AutoEventWireup</span><span class="code-keyword">="</span><span class="code-keyword">true"</span><span class="code-attribute"> CodeFile</span><span class="code-keyword">="</span><span class="code-keyword">is.aspx.cs"</span><span class="code-attribute"> Inherits</span><span class="code-keyword">="</span><span class="code-keyword">_is"</span><span class="code-attribute"> CodeFileBaseClass</span><span class="code-keyword">="</span><span class="code-keyword">BasePage"</span><span class="code-attribute"> Title</span><span class="code-keyword">="</span><span class="code-keyword">Effective Customer Feedback Management, Improve Customer Commmunication"</span><span class="code-attribute"> Meta_Keywords</span><span class="code-keyword">="</span><span class="code-keyword">Customer Feedback, Customer Opinion, feedback, opinion, idea, ideas, idea management, customer feedback management, product management, product manager, product marketing, product marketing manager"</span><span class="code-attribute"> Meta_Description</span><span class="code-keyword">="</span><span class="code-keyword">IdeaScope is an on-demand and embedded solution that allows you to capture, prioritize and centrally manage customer feedback. Make your customer feedback process more efficient. Save time and involve more stakeholders without significant cost."</span><span class="code-attribute"> </span><span class="code-pagedirective">%></span> </pre> </div> <p class="article-text"> Without the regular expression replacement, these <span class="search-highlight">tags</span> would contain new lines and excess spaces at the beginning and ending of each line. I suspect that some search engine spiders might get upset about that, and the primary reason for including these <span class="search-highlight">tags</span> is to make search engines happy.</p> <p class="article-text"> There is one last thing that you might want to do to tidy up this solution. The <span class="search-highlight">ASP.NET</span> validation in Visual Studio 2005 is not going to recognize <strong>Meta_Keywords</strong> or <strong>Meta_Description</strong>. You will get warnings from the compiler saying that these are not valid attributes for the @Page directive. You will also see those red squiggley lines under those attributes in Visual Studio. Your code will compile and run fine. If you are like me and don't want to see any warnings or validation errors, then you will want to add the following lines to Visual Studio's schema for the @Page directive. </p> <div class="pre-action-link" id="premain10" style="display: block;" width="100%"> <img height="9" id="preimg10" preid="10" src="http://www.codeproject.com/images/minus.gif" style="cursor: pointer;" width="9" /><span id="precollapse10" preid="10" style="cursor: pointer; margin-bottom: 0px;"> Collapse</span><span> | </span><a href="http://www.codeproject.com/Articles/17368/Using-Meta-Tags-with-Master-Pages-in-ASP-NET#" preid="10">Copy Code</a></div> <div class="article-text"> <pre id="pre10" lang="xml" style="margin-top: 0px;"> <span class="code-keyword"><</span><span class="code-leadattribute">xsd:attribute</span> <span class="code-attribute">name</span><span class="code-keyword">="</span><span class="code-keyword">Meta_Keywords"</span> <span class="code-attribute">vs:nonfilterable</span><span class="code-keyword">="</span><span class="code-keyword">true"</span> <span class="code-keyword">/</span><span class="code-keyword">></span> <span class="code-keyword"><</span><span class="code-leadattribute">xsd:attribute</span> <span class="code-attribute">name</span><span class="code-keyword">="</span><span class="code-keyword">Meta_Description"</span> <span class="code-attribute">vs:nonfilterable</span><span class="code-keyword">="</span><span class="code-keyword">true"</span> <span class="code-keyword">/</span><span class="code-keyword">></span> </pre> </div> <p class="article-text"> These nodes should be inserted as child nodes of <code lang="xml"><xsd:complexType name="PageDef"></code> The schema file is located at the following location if you installed Visual Studio 2005 in the default location: </p> <div class="article-text"> <code>C:\Program Files\Microsoft Visual Studio 8\Common7\Packages\schemas\html\page_directives.xsd </code></div> <p class="article-text"> This article demonstrates how to extend the @Page directive to support meta keywords and meta descriptions. You can easily add other meta <span class="search-highlight">tags</span> to the sample code. Complete source files and a sample project are included for C# and VB. Thanks to Scott Guthrie's blog post, <a href="http://weblogs.asp.net/scottgu/archive/2005/08/02/421405.aspx" target="_blank">Obsure but cool feature in <span class="search-highlight">ASP.NET</span> 2.0</a> for the information that led up to this solution.</p> <p class="article-text"> Your comments and suggestions to improve this article are welcome.</p> <h2> License</h2> <div id="LicenseTerms"> <p> This article, along with any associated source code and files, is licensed under <a href="http://www.codeproject.com/info/cpol10.aspx" rel="license">The Code Project Open License (CPOL)</a></p> </div> <p> <br /></p> </form> <p>  <br /></p>

No comments:

Post a Comment

Google Ads by Mavra

About Me

My photo
Jeddah, Makkah, Saudi Arabia
for more details www.inhousetoday.com