Using SiteMap and MasterPages to set META Tags in ASP.NET and C#

Category: ASP.NET

Using the SiteMap for SEO Purposes in ASP.NET 3.5 and C#

Especially since the introduction of Web 2.0, a new emphasis has been made on SEO (Search Engine Optimization) for our web pages. Google, amongst others, have managed to organize the world's data so well, that it is becoming more and more important to adhere to such organization techniques to gain the traffic your web site deserves.

In this article we will look at one of the ways we can use the SiteMap in ASP.NET to organize our Web Page more efficiently, and also make it easier for us to update. We will be creating a SiteMap file, which is written in XML, to define our meta Title, Description, Keywords tags as well as page Headers for each of our Web Pages depending upon their location.

What we will learn in this article:

  • How to utilize an ASP.NET SiteMap to harness the power of SEO
  • How to make it easier for us to maintain a multi-page Web Site using a SiteMap

Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!

Getting Started
The major search engines rank a page against serach results based upon many factors. Having matching terms in your meta tags will rank your page higher, particularly in Title, Description and Keywords. However, when maintaining larger sites, this can be difficult to keep organized and a hefty chore to maintain and update. However, we will show how we can make it a little easier using the SiteMap.

To begin, start a new Web Site project in Visual Studio.NET - in this example, we will be using 2008, but 2005 will work just fine.
Once open, right-click your project in the Solution Explorer and choose Add New Item.. SiteMap. Leave the name as Web.sitemap and click Ok. You should have something like this:

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.

This is where we start off. But first, let's create some pages to work with. First, let's add a MasterPage: right-click your project and choose Add New Item.. MasterPage. Leave the name as MasterPage.master and then delete the Default.aspx that was added when you created the project.
Now we can add a new Web Form, making sure we check the box to select a Master Page:

[Click to view full-size]

Next, choose the MasterPage we just created:

[Click to view full-size]

Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!

Our next step is to design our MasterPage. For the purpose of this tutorial, we will simply add a Literal control as a placeholder for the page header:

We will also add three more Web Forms to our Project, namely Page1.aspx, Page2.aspx and Page3.aspx and we will identify them in the body, and link to all other pages, like so:

This is Page1.aspx


Try Server Intellect for Windows Server Hosting. Quality and Quantity!

We make sure that we check MasterPage for each of our Web Forms.
The Literal control in our MasterPage is going to display the page header that we specify for each page in the SiteMap. Let's go ahead and begin our SiteMap. It will have somewhat of a hierarchical structure like so:

Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.

Notice our attributes for each page in our project. We will be using title for the Meta Title tag, PageHeader for our Literal control, description for the Meta description tag, and keywords for the Meta keywords tag.
To implement this, we will add code-behind to the Master Page:

protected void Page_Load(object sender, EventArgs e)
if (SiteMap.CurrentNode["PageHeader"] != null)
lit_PageHeader.Text = SiteMap.CurrentNode["PageHeader"].ToString();

if (SiteMap.CurrentNode != null)
Page.Title = SiteMap.CurrentNode.Title;

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

if (SiteMap.CurrentNode.Description != null)
HtmlMeta meta = new HtmlMeta();
meta.Name = "Description";
meta.Content = SiteMap.CurrentNode.Description.ToString();

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

Now when we run Default.aspx we should be presented with the following in the Title Bar:

And we should also be presented with the Page Header from the Web.sitemap:

Also note that the META tags in the source are populated:

What we have Learned

We have learned that we can use the SiteMap to easily managed and update our Meta tags for each of our pages within our site.


Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!

100% SPAM FREE! We will never sell or rent your email address!