Building sitemap.xml for SEO ASP.NET MVC

Creating a sitemap.xml file for Google and other search engines can be accomplished in MVC using a simple ActionResult that returns the appropriate XML blog. The problem however is in generating the list of URLs to go into that sitemap.xml file.

In ASP.NET MVC there is no distinction between an action method that is a page and one that is a service call. To remedy that let’s create an ActionFilterAttribute that can be applied to any method to mark it as a page and to record the URL that we want that page to show up as in our sitemap:

/// \<summary\>
/// This attribute indicates that a method is an actual page and gives the data for it
/// \</summary\>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public class MVCUrlAttribute : ActionFilterAttribute
    public string Url { get; private set; }

    public MVCUrlAttribute(string url)
    this.Url = url;

    public override void OnResultExecuting(ResultExecutingContext filterContext)
    string fullyQualifiedUrl = filterContext.HttpContext.Request.Url.GetLeftPart(UriPartial.Authority) + this.Url;
    // We build HTML here because we want the View to be easily able to include it without any conditionals
    // and because the ASP.NET WebForms view engine sometimes doesn't subsitute \<% in certain head items
    filterContext.Controller.ViewData["CanonicalUrl"] = @"\<link rel=""canonical"" href=""" + fullyQualifiedUrl + " /\>";

You may wonder at this point why we can’t just use the Routing table to figure this out. The issue is that multiple routes may map onto one page but we still want it to show up just once in the sitemap otherwise we will get slammed for duplicate content. We also want to be able to mark each page with its canonical URL.

Now we can use reflection to find all of the ‘pages’ in our ASP.NET MVC Application and then build a sitemap.xml file from them.

    List<string> allPageUrls = new List\<string\>();
    // Find all the MVC Routes
    var allControllers = Assembly.GetExecutingAssembly().GetTypes().Where(t =\> t.IsSubclassOf(typeof(Controller)));
    Log.DebugFormat("Found {0} controllers", allControllers.Count());
    foreach (var controllerType in allControllers)
      var allPublicMethodsOnController = controllerType.GetMethods(BindingFlags.Public | BindingFlags.Instance);
      Log.DebugFormat("Found {0} public methods on {1}", allPublicMethodsOnController.Count(), controllerType.Name);
      foreach (var publicMethod in allPublicMethodsOnController)
        var mvcurlattr = publicMethod.GetCustomAttributes(true).OfType\<MVCUrlAttribute\>().FirstOrDefault();
        if (mvcurlattr != null)
          string url = mvcurlattr.Url;
          Log.Debug("Found " + controllerType.Name + "." + publicMethod.Name + " \<-- " + url);

Tue Feb 09 2010 23:30:58 GMT-0800 (Pacific Standard Time)

Next page: ASP.NET MVC 2 and the Ambiguous Match Exception for Action methods with different signatures

Previous page: Useful Twitter links Feb 8-Feb 15 2010