in .NET

How to create dynamic XML sitemaps in ASP.NET MVC

When developing web applications (especially websites), some rules have to be obeyed. One of those rules are Sitemaps. Sitemap is an essential to tell more about a website to search engines. According to sitemaps.org, sitemaps are an easy way for webmasters to inform search engines about pages on their sites:

Sitemaps are an easy way for webmasters to inform search engines about pages on their sites that are available for crawling. In its simplest form, a Sitemap is an XML file that lists URLs for a site along with additional metadata about each URL (when it was last updated, how often it usually changes, and how important it is, relative to other URLs in the site) so that search engines can more intelligently crawl the site.

Implement sitemaps in ASP.NET MVC is very easy and this post is going to cover it.

There are several ways to implement sitemaps in ASP.NET MVC. Since sitemaps are just XML files, they can be generated using XLINQ (LINQ to XML) technology.

The process of creating a sitemap in a website can be divided into the following steps:

  • Get a list of URLs which has to be included in the sitemap.
  • Create the sitemap XML in code.
  • Return the created XML to the user.

Note: You don’t really need to create an actual XML file. All of the process above can be done inside a controller action.

The first step is to get the list of objects we want to create the sitemap based on. In this post, I have a list of categories which have a unique URL based on its ID. So:

public ActionResult SiteMap() {
    var items = categoryService.SelectAll();
}

After that, it’s time to create the XML document. Sitemaps must have a specific format; so, they can recognized by search engines. The following is a valid XML sitemap:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
   <url>
      <loc>http://www.example.com/</loc>
      <lastmod>2005-01-01</lastmod>
      <changefreq>monthly</changefreq>
      <priority>0.8</priority>
   </url>
</urlset>

As you know, creating such XML file in LINQ to XML is a very simple task to do:

public ActionResult SiteMap() {
    XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
    const string url = "http://example.com/Category/{0}";
    var items = categoryService.SelectAll();
    var sitemap = new XDocument(
    new XDeclaration("1.0", "utf-8", "yes"),
    new XElement(ns + "urlset",
        from i in items
        select
        new XElement(ns + "url",
            new XElement(ns + "loc", string.Format(url, i.CategoryId)),
            new XElement(ns + "lastmod", String.Format("{0:yyyy-MM-dd}", DateTime.Now)),
            new XElement(ns + "changefreq", "always"),
            new XElement(ns + "priority", "0.5")
    )));
}

As you can see in above code, we’ve simply created an XML file in sitemap format. Now we can return it to the user.

return Content("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + sitemap.ToString(), "text/xml");

Look at the code above. When creating normal actions, we usually return a View or PartialView objects; but, since we’re going to return a content, we use the Content method. Content method requires two arguments. The first argument is the content we’re going to return to the user as string and the second is the content’s MIME type.

Hope it helps.