SEO (search engine optimization) is all the rage these days. There are many great sites on the web that nobody’s ever seen or heard of. Part of the reason might be that the sites haven’t been optimized for search engines. I’ll do another post all about SEO some day, but for right now I want to highlight a couple of ways that you can enhance the pages on your site.
All good ASP .NET sites have a sitemap that organizes the site. If you don’t have a CMS (content management system), this is also a great place to store meta information about each page, such as the description and key words. These can be added as attributes on the sitemap, along with any other possible meta tags. I created a user control that I like to use that handles meta tags, and I just drop this into the master pages on the sites that I create. As long as I have a solid sitemap, the control takes care of the rest. I also like to take care of the page title for each page in this control, it’s handled in the code behind.
As you can see in the following sitemap file, I’ve used the description attribute, and also added my own keyWords attribute. Although key words aren’t as popular as they used to be for SEO, I like to go ahead and add them if I have a few handy. If you don’t list any key words, the control just ignores that attribute.
Here is the markup for the control, looks very straightforward, doesn’t it? Notice that the visibility on all the controls is initially set to false, because not all pages are going to have all of these attributes.
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="MetaTags.ascx.cs" Inherits="usercontrols_MetaTags" %>
Then in the code behind, I look at the attributes on the current sitemapnode and determine which tags to enable. This control is best used in conjunction with a master page, so that you only need to add this once, and any page using the master page will automatically be taken care of. Also notice that we use the title attribute on the sitemap to set the page title. I store a site name in the configuration that I prepend to each title. Not sure if that’s a best practice these days, just a preference of mine.
protected void Page_Load(object sender, EventArgs e)
{
//First, check to see that we even need to handle this page, pages not in the sitemap will not have meta tags
if (System.Web.SiteMap.CurrentNode != null)
{
this.Page.Title = ConfigurationManager.AppSettings["SiteName"] + " - " + System.Web.SiteMap.CurrentNode.Title;
if (System.Web.SiteMap.CurrentNode.Description != null && !string.IsNullOrEmpty(System.Web.SiteMap.CurrentNode.Description))
{
metaDescription.Visible = true;
metaDescription.Attributes.Add("content", System.Web.SiteMap.CurrentNode.Description);
}
if (System.Web.SiteMap.CurrentNode["keyWords"] != null && !string.IsNullOrEmpty(System.Web.SiteMap.CurrentNode["keyWords"]))
{
metaKeywords.Visible = true;
metaKeywords.Attributes.Add("content", System.Web.SiteMap.CurrentNode["keyWords"]);
}
if (System.Web.SiteMap.CurrentNode["robots"] != null && !string.IsNullOrEmpty(System.Web.SiteMap.CurrentNode["robots"]))
{
metaRobots.Visible = true;
metaRobots.Attributes.Add("content", System.Web.SiteMap.CurrentNode["robots"]);
}
if (System.Web.SiteMap.CurrentNode["author"] != null && !string.IsNullOrEmpty(System.Web.SiteMap.CurrentNode["author"]))
{
metaAuthor.Visible = true;
metaAuthor.Attributes.Add("content", System.Web.SiteMap.CurrentNode["author"]);
}
if (System.Web.SiteMap.CurrentNode["copyright"] != null && !string.IsNullOrEmpty(System.Web.SiteMap.CurrentNode["copyright"]))
{
metaCopyright.Visible = true;
metaCopyright.Attributes.Add("content", System.Web.SiteMap.CurrentNode["copyright"]);
}
}
else
{
//At the very least, set a default page title
this.Page.Title = ConfigurationManager.AppSettings["SiteName"];
}
}
Although I rarely use, or never use, a couple of those meta tags, I like to have them there just in case. The result of using this control in conjunction with the sitemap is something that looks like the following in the head section your rendered page source:
WordPress is suppressing the title tag, but just imagine the following in the code snippet above <title>My Site name – Home Page</title>
Recent Comments