#6 Centering Yin & Yang by Centralizing Meta-Data
ASP.NET 2.0's ability to store arbitrary page information centrally can solve a great number of problems. For instance, consider the simplicity of maintaining meta-keywords and meta-descriptions in the
Web.sitemap rather than distributed across every page. The new approach makes it simple to insert such metadata in the rendered HTML.
<head>
<title><%# mstrTitle %></title>
<meta name="description" content="<%# mstrMetaDescription %>" />
<meta name="keywords" content="<%# mstrMetaKeywords %>" />
Then simply declare and set a couple of protected properties in the code-behind:
protected string mstrMetaKeywords = "";
protected string mstrMetaDescription = "";
protected string mstrTitle = "";
protected void Page_Load(object sender, EventArgs e) {
if (SiteMap.CurrentNode != null) {
if (SiteMap.CurrentNode["metaKeywords"] != null)
mstrMetaKeywords = SiteMap.CurrentNode["metaKeywords"];
mstrMetaDescription = SiteMap.CurrentNode.Description;
mstrTitle = SiteMap.CurrentNode.Title;
}
...
if (!Page.IsPostBack)
Page.DataBind();
}
Don't forget to check whether the
CurrentNode property or any custom-defined SiteMapNode attributes are null. Lastly add
metaKeywords and
description attributes to your
Web.sitemap file:
 | |
Figure 3: Displaying TreeView Navigation: The Maintenance page in the solution accompanying this article contains a TreeView that links to a SiteMapDataSource and acts as a site map. |
<siteMapNode
url="~/SiteMap.aspx"
title="Site Map"
metaKeywords="Site Map"
description="The Site Map provides links to every
page on the site organized in a logical hierarchy." />
This approach vastly simplifies maintaining page-level metadata used by internal or external search engines. By combining the technique with a Master Page, you may just write your Web administrator out of a job!
#7: Sitemap Simplicity
Adding a functional site map to your site is now as easy as adding a SiteMapDataSource and a TreeView to your Web form, and then setting the
Treeview.DataSourceID property to the SiteMapDataSource. To hide the root node just set the
SiteMapDataSource.ShowStartingNode Property to
false. You can make the TreeView a little prettier by setting the
ShowExpandCollapse property to
false and
ShowLines to
true. See
Figure 3.
Don't Miss the Next Installment!
This article demonstrated seven problems and their solutions using basic ASP.NET 2.0 site navigation techniques in the context of scenarios that developers are most likely to encounter. The second installment in this article series provides two more problems and solutions, and introduces more advanced techniques, including: hiding unauthorized pages, extending the site map provider model to include dynamic content, and caching dynamic content using ASP.NET 2.0's new SqlCacheDependency class.