Colin Cochrane

Colin Cochrane is a Software Developer based in Victoria, BC specializing in C#, PowerShell, Web Development and DevOps.

Using the ASP.NET Web.Sitemap to Manage Meta Data

kick it on DotNetKicks.com

In an ASP.NET application the web.sitemap is very convenient tool for managing the site's structure, especially when used with an asp:Menu control.  One aspect of the web.sitemap that is often overlooked is the ability to add custom attributes to the <siteMapNode> elements, which provides very useful leverage for managing a site's meta-data.  I think the best way to explain would be through a simple example.

Consider a small site with three pages: /default.aspx, /products.aspx, and /services.aspx. The web.sitemap for this site would look like this:

<?xml version="1.0" encoding="utf-8" ?>

<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >

<siteMapNode url="~/" title="Home">

     <siteMapNode url="~/products.aspx" title="Products" />
     <siteMapNode url="~/services.aspx" title="Services" />

</siteMapNode>

 

Now let's add some custom attributes where we can set the Page Title (because the title attribute is where the asp:Menu control looks for the name of a menu item and it's probably best to leave that as is), the Meta Description, and the Meta Keywords elements. 

 

<?xml version="1.0" encoding="utf-8" ?>

<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >

<siteMapNode url="~/" title="Home" pageTitle="Homepage" metaDescription="This is my homepage!" metaKeywords="homepage, keywords, etc">

     <siteMapNode url="~/products.aspx" title="Products" pageTitle="Our Products" metaDescription="These are our fine products" metaKeywords="products, widgets"/>
     <siteMapNode url="~/services.aspx" title="Services" pageTitle="Our Services" metaDescription="Services we offer" metaKeywords="services, widget cleaning"/>

</siteMapNode>

 

Now with that in place all we need is a way to access these new attributes and use them to set the elements on the pages.  This can be accomplished by adding a module to your project, we'll call it "MetaDataFunctions" for this example.  In this module you add the following procedure.

 

Public Sub GenerateMetaTags(ByVal TargetPage As Page)
    Dim head As HtmlHead = TargetPage.Master.Page.Header
    Dim meta As
New HtmlMeta

    If SiteMap.CurrentNode IsNot Nothing Then
      meta.Name = "keywords"
      meta.Content = SiteMap.CurrentNode("metaKeywords")

      head.Controls.Add(meta)

      meta =
New HtmlMeta
      meta.Name = "description"
      meta.Content = SiteMap.CurrentNode("metaDescription")
      head.Controls.Add(meta)

      TargetPage.Title = SiteMap.CurrentNode.Description
   
Else

      meta.Name = "keywords"
      meta.Content = "default keywords"

      head.Controls.Add(meta)

      meta =
New HtmlMeta
      meta.Name = "description"
      meta.Content = "default description"

      head.Controls.Add(meta)

      TargetPage.Title = "default page title"
    End If
  End Sub

 

Then all you have to do is call this procedure on the Page_Load event like so...

 

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

    GenerateMetaTags(Me)    

End Sub

 

..and you'll be up and running.  Now you have a convenient, central location where you can see and manage your site's meta-data.

Comments (3) -

  • EGV

    2/19/2008 3:12:08 AM | Reply


    Here's the C# code (NOTE: I only tested this for the "pageTitle" functionality):


    This line goes n2 page_load:
        GenerateMetaTags(this.Page);



        public void GenerateMetaTags(Page TargetPage)
        {
            HtmlHead head = TargetPage.Master.Page.Header;
            HtmlMeta meta = new HtmlMeta();

            if (SiteMap.CurrentNode != null)
            {
                meta.Name = "keywords";
                meta.Content = SiteMap.CurrentNode["metaKeywords"];

                head.Controls.Add(meta);
                meta = new HtmlMeta();

                meta.Name = "description";
                meta.Content = SiteMap.CurrentNode["metaDescription"];
                head.Controls.Add(meta);

                TargetPage.Title = SiteMap.CurrentNode["pageTitle"].ToString();
            }
            else
            {
                meta.Name = "keywords";
                meta.Content = "default keywords";

                head.Controls.Add(meta);

                meta = new HtmlMeta();
                meta.Name = "description";
                meta.Content = "default description";

                head.Controls.Add(meta);

                TargetPage.Title = "default page title";
            }
        }

  • Colin Cochrane

    2/19/2008 4:10:19 AM | Reply

    Thanks for the translation EGV.  Much appreciated!

  • luigi

    4/9/2008 6:00:14 PM | Reply

    thank you again Colin another great job from you.
    I've resolved this problem in another manner but i'm thinking about changing the code in this way.
    Thanks a lot.
    Don't Stop!

Pingbacks and trackbacks (1)+

Loading