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

by Colin Cochrane 10/30/2007 6:48:00 PM
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.

Tags: , ,

SEO | ASP.NET | Web Development

Comments (3) -

2/19/2008 11:12:08 AM


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";
        }
    }

EGV United States

2/19/2008 12:10:19 PM

Thanks for the translation EGV.  Much appreciated!

Colin Cochrane Canada

4/10/2008 2:00:14 AM

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!

luigi Italy

Pingbacks and trackbacks (1)+

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Powered by BlogEngine.NET 2.5.0.6

All Content and Intellectual Property is under Copyright Protection | Colin Cochrane ©2007

About the author

Colin Cochrane

Colin Cochrane

SEO and ASP.NET Developer.

Recent comments

Recent posts

Archive

Authors

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer. © Copyright Colin Cochrane 2014

Sign in