27

I was wondering if anyone has done this yet or has any examples on how to create a Google Sitemap for an MVC website.

Any help or example would be appreciated.

Im talking about this: https://www.google.com/webmasters/tools/docs/en/protocol.html

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794

5 Answers5

21

I used Mike Brind's Sitemap code, with a small change.

You need to add the XNamespace to every XElement, otherwise Google spits the dummy.

Here's my version:

public ContentResult Index()
        {
            XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
            const string url = "http://www.website.com/controller/action/{0}";
            var items = _db.DataAccessHere();
            var sitemap = new XDocument(
                new XDeclaration("1.0", "utf-8", "yes"),
                new XElement(ns + "urlset",
                    from i in items
                    select
                    //Add ns to every element.
                    new XElement(ns + "url", 
                      new XElement(ns + "loc", string.Format(url, i.ItemID)),
                          new XElement(ns + "lastmod", String.Format("{0:yyyy-MM-dd}", i.DateAddedUTC)),
                      new XElement(ns + "changefreq", "monthly"),
                      new XElement(ns + "priority", "0.5")
                      )
                    )
                  );
            return Content(sitemap.ToString(), "text/xml");
        }

Credit to Mike for posting the original article and code.

Jeremy Cade
  • 1,351
  • 2
  • 17
  • 28
  • 1
    Ta, Nice piece of code. You might want to add another XElement of the root site node. – Rebecca Jan 09 '11 at 17:35
  • great and simple piece of code, I used this and created http://www.gprprojecten.nl/Home/Sitemap with the addition that @Junto already proposed – Daniël Tulp Apr 09 '13 at 08:44
  • Thanks - again - to Mike. His website is at http://www.mikesdotnetting.com, and his articles are simple to understand and powerful. And no, I have no connection with him at all other than that I want to say thank you! – Andy Brown Jul 08 '14 at 16:09
  • Thank you so much to you and #MikeBrind for how to create a sitemap xml dynamically. I have one doubt. So we created site map xml file, now **how can I update that automatically in Google Web Master**. I mean, is there any `Google Web Master Api`, so that I can call that and update my sitemap.xml there ? Or I need to **manually take this file and update there every time** ? – Basanta Matia Jul 28 '17 at 13:28
7

Shameless self plug: I created a library called SimpleMvcSitemap after having weird issues with MvcSiteMapProvider on production. You can serve sitemap files from any action method without any configuration:

public class SitemapController : Controller
{
    public ActionResult Index()
    {
        List<SitemapNode> nodes = new List<SitemapNode>
        {
            new SitemapNode(Url.Action("Index","Home")),
            new SitemapNode(Url.Action("About","Home")),
            //other nodes
        };

        return new SitemapProvider().CreateSitemap(nodes);
    }
}

It also supports all the Google Sitemap extensions available.

Ufuk Hacıoğulları
  • 37,978
  • 12
  • 114
  • 156
  • SimpleMVCSutemap project will create sitemap file dynamically into project after this line execution `return new SitemapProvider().CreateSitemap(HttpContext, nodes);` ? – Thomas Nov 06 '15 at 14:25
  • @Thomas it's generated on every request that calls this action method. You can save it in a file and serve it as content if your links are static. – Ufuk Hacıoğulları Nov 06 '15 at 14:29
5

The easiest way would be to use any one of a number of free sitemap builders out there - they will crawl your site, follow links, and generate a sitemap XML file for you.

Here's one for example

Eric Petroelje
  • 59,820
  • 9
  • 127
  • 177
3

Here's a post that might give you some ideas. Basically it generates a sitemap from route values.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
2

so here's the thing, using generators will just about create a link for "everything" in your site. So if you have, let's say a card site, and you have like a hundred thousand card items, each with it's own link and all, you'll likely see the same amount of links. If you want that, then xml sitemap generators are the way to go.

But if you want it a little bit personalized, you can do these:

List all major sections of your sites. This is easy to do considering that most MVCs are using the "clean URLs" sort of thing. kinda like "site.com/items/phones"

Create an XML document, depending on the language you're using.

At the minimum, you should have a document like this:

<?xml version="1.0" encoding="utf-8"?> 
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> 
      <url> 
        <loc>http://dragonflysco.com/catalog/finishings/19</loc> 
      </url> 
      <!-- more here -->
    </urlset>

For more advanced structure, check this: http://www.google.com/support/webmasters/bin/answer.py?answer=183668

yretuta
  • 7,963
  • 17
  • 80
  • 151