Created
January 21, 2018 15:35
-
-
Save pbres/6016f1d1ded597836f96dec5f8933ffc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SitemapController : RenderMvcController | |
{ | |
public ActionResult SitemapXml() | |
{ | |
var startNode = CurrentPage.Site(); | |
if (startNode != null) | |
{ | |
var sitemapService = new UmbracoSitemapService(); | |
var items = sitemapService.GetItemsAsList(startNode.Id); | |
XNamespace xn = "http://www.sitemaps.org/schemas/sitemap/0.9"; | |
var doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes")); | |
var urlSet = new XElement(xn + "urlset"); | |
urlSet.Add(new XAttribute("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9")); | |
foreach (var item in items) | |
{ | |
urlSet.Add(new XElement(xn + "url", new XElement(xn + "loc", item.Url), | |
new XElement(xn + "lastmod", item.LastUpdateDate))); | |
} | |
doc.Add(urlSet); | |
return Content(String.Concat(doc.Declaration.ToString(), "\r\n", doc.ToString()), "text/xml"); | |
} | |
return new HttpNotFoundResult("400"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment