Last active
January 4, 2018 16:27
-
-
Save pedroadaodev/66e5065603d553d489ec4ffd349df82a to your computer and use it in GitHub Desktop.
Sitemap.xml and robots.txt with Umbraco
This file contains 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
User-agent: * | |
Sitemap: https://www.mywebsite.com/Sitemap.ashx |
This file contains 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
using MyWebsite.FrontEnd.Models; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Web; | |
using Umbraco.Core.Models; | |
using Umbraco.Web; | |
namespace MyWebsite.FrontEnd | |
{ | |
/// <summary> | |
/// Summary description for sitemap | |
/// </summary> | |
public class sitemap : IHttpHandler | |
{ | |
public void ProcessRequest(HttpContext context) | |
{ | |
context.Response.ContentType = "text/xml"; | |
context.Response.ContentEncoding = System.Text.Encoding.UTF8; | |
context.Response.Expires = -1; | |
context.Response.Cache.SetAllowResponseInBrowserHistory(true); | |
StringBuilder sb = new StringBuilder(); | |
sb.AppendLine("<?xml version='1.0' encoding='UTF-8'?><urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\">"); | |
var helper = new UmbracoHelper(UmbracoContext.Current); | |
IPublishedContent rootNode = helper.ContentAtRoot().First(); | |
foreach (var item in rootNode.Children<Models.Homepage>()) // get my homepage | |
{ | |
sb.AppendLine(Traverse(item)); | |
} | |
sb.AppendLine("</urlset>"); | |
context.Response.Write(sb.ToString()); | |
} | |
private string Traverse(IPublishedContent node) | |
{ | |
//@*Update the level to reflect how deep you want the sitemap to go *@ | |
var maxLevelForSitemap = 6; | |
//@*Select visible children *@ | |
var selection = node.Children<Models.PageDefault>().Where("Visible").Where("Level <= " + maxLevelForSitemap); // get all my pages of docType "PageDefault" | |
//@*If any items are returned, render a list *@ | |
StringBuilder sb = new StringBuilder(); | |
if (selection.Any()) | |
{ | |
foreach (var item in selection) | |
{ | |
sb.AppendLine(string.Format("<url><loc>{0}</loc><lastmod>{1}</lastmod></url>", item.UrlWithDomain(), item.CreateDate.ToString("yyyy/MM/dd"))); | |
//@*Run the traverse helper again for any child pages *@ | |
sb.AppendLine(Traverse(item)); | |
} | |
} | |
return sb.ToString(); | |
} | |
public bool IsReusable | |
{ | |
get | |
{ | |
return false; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment