Last active
December 17, 2015 14:48
-
-
Save pksorensen/5626622 to your computer and use it in GitHub Desktop.
BlogSitemap.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
<%@ WebHandler Language="C#" Class="BlogSitemap" %> | |
using System; | |
using System.Globalization; | |
using System.Text; | |
using System.Web; | |
using System.Linq; | |
using Composite.Data; | |
using Composite.Data.Types; | |
using Composite.Core.Routing; | |
using Composite.Community.Blog; | |
public class BlogSitemap : IHttpHandler | |
{ | |
protected string Changefreq = "monthly"; | |
public void ProcessRequest(HttpContext context) | |
{ | |
Guid pageId = Guid.Empty; | |
if (context.Request["bid"] != null && Guid.TryParse(context.Request["bid"], out pageId)) | |
{ | |
var cultureName = context.Request["cultureName"]; | |
if (string.IsNullOrEmpty(cultureName)) | |
{ | |
cultureName = DataLocalizationFacade.DefaultLocalizationCulture.Name; | |
} | |
var cultureInfo = new CultureInfo(cultureName); | |
context.Response.ContentType = "text/xml"; | |
var xml = new StringBuilder(); | |
xml.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); | |
xml.Append("<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">"); | |
var cultures = DataLocalizationFacade.IsLocalized(typeof(IPage)) ? DataLocalizationFacade.ActiveLocalizationCultures : new[] { CultureInfo.InvariantCulture }; | |
using (var conn = new DataConnection(cultureInfo)) | |
{ | |
string pageUrl = BlogFacade.GetPageUrlById(pageId); | |
if (!string.IsNullOrEmpty(pageUrl)) | |
{ | |
foreach (var blog in conn.Get<Entries>().Where(b => b.PageId == pageId)) | |
{ | |
var blogurl =GetFullPath( BlogFacade.GetBlogUrl(blog.Date, blog.Title, blog.PageId, pageUrl)); | |
xml.Append("<url>"); | |
xml.AppendFormat("<loc>{0}</loc>", blogurl); | |
xml.AppendFormat("<lastmod>{0}</lastmod>", blog.Date.ToString("yyyy-MM-dd")); | |
xml.AppendFormat("<changefreq>{0}</changefreq>", Changefreq); | |
xml.AppendFormat("<priority>{0}</priority>", "0.5"); | |
xml.Append("</url>"); | |
} | |
} | |
} | |
xml.Append("</urlset> "); | |
context.Response.Write(xml); | |
} | |
else | |
{ | |
context.Response.Write("The required query paramerer 'bid' (blog page GUID) is missing."); | |
} | |
} | |
public bool IsReusable | |
{ | |
get | |
{ | |
return false; | |
} | |
} | |
internal string GetFullPath(string path) | |
{ | |
var request = HttpContext.Current.Request; | |
return (new Uri(request.Url, System.IO.Path.Combine(request.ApplicationPath, path))).ToString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment