how to create google sitemap for mvc site?
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.
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.
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.