PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/BlogEngine/DotNetSlave.BusinessLogic/Web/HttpHandlers/SiteMap.cs

#
C# | 109 lines | 65 code | 15 blank | 29 comment | 2 complexity | a5866004938df6874486fe13baec6eaf MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. namespace BlogEngine.Core.Web.HttpHandlers
  2. {
  3. using System;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Xml;
  8. /// <summary>
  9. /// A blog sitemap suitable for Google Sitemap as well as
  10. /// other big search engines such as MSN/Live, Yahoo and Ask.
  11. /// </summary>
  12. public class SiteMap : IHttpHandler
  13. {
  14. #region Properties
  15. /// <summary>
  16. /// Gets a value indicating whether another request can use the <see cref = "T:System.Web.IHttpHandler"></see> instance.
  17. /// </summary>
  18. /// <value></value>
  19. /// <returns>true if the <see cref = "T:System.Web.IHttpHandler"></see> instance is reusable; otherwise, false.</returns>
  20. public bool IsReusable
  21. {
  22. get
  23. {
  24. return false;
  25. }
  26. }
  27. #endregion
  28. #region Implemented Interfaces
  29. #region IHttpHandler
  30. /// <summary>
  31. /// Enables processing of HTTP Web requests by a custom HttpHandler that
  32. /// implements the <see cref="T:System.Web.IHttpHandler"></see> interface.
  33. /// </summary>
  34. /// <param name="context">
  35. /// An <see cref="T:System.Web.HttpContext"></see>
  36. /// object that provides references to the intrinsic server objects
  37. /// (for example, Request, Response, Session, and Server) used to service HTTP requests.
  38. /// </param>
  39. public void ProcessRequest(HttpContext context)
  40. {
  41. using (var writer = XmlWriter.Create(context.Response.OutputStream))
  42. {
  43. writer.WriteStartElement("urlset", "http://www.google.com/schemas/sitemap/0.84");
  44. // Posts
  45. foreach (var post in Post.Posts.Where(post => post.IsVisibleToPublic))
  46. {
  47. writer.WriteStartElement("url");
  48. writer.WriteElementString("loc", post.AbsoluteLink.ToString());
  49. writer.WriteElementString(
  50. "lastmod", post.DateModified.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
  51. writer.WriteElementString("changefreq", "monthly");
  52. writer.WriteEndElement();
  53. }
  54. // Pages
  55. foreach (var page in Page.Pages.Where(page => page.IsVisibleToPublic))
  56. {
  57. writer.WriteStartElement("url");
  58. writer.WriteElementString("loc", page.AbsoluteLink.ToString());
  59. writer.WriteElementString(
  60. "lastmod", page.DateModified.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
  61. writer.WriteElementString("changefreq", "monthly");
  62. writer.WriteEndElement();
  63. }
  64. // Removed for SEO reasons
  65. //// Archive
  66. // writer.WriteStartElement("url");
  67. // writer.WriteElementString("loc", Utils.AbsoluteWebRoot.ToString() + "archive.aspx");
  68. // writer.WriteElementString("lastmod", DateTime.Now.ToString("yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture));
  69. // writer.WriteElementString("changefreq", "daily");
  70. // writer.WriteEndElement();
  71. // Contact
  72. writer.WriteStartElement("url");
  73. writer.WriteElementString("loc", string.Format("{0}contact.aspx", Utils.AbsoluteWebRoot));
  74. writer.WriteElementString("lastmod", DateTime.Now.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
  75. writer.WriteElementString("changefreq", "monthly");
  76. writer.WriteEndElement();
  77. // Blog
  78. if (Page.GetFrontPage() != null)
  79. {
  80. writer.WriteStartElement("url");
  81. writer.WriteElementString("loc", string.Format("{0}blog.aspx", Utils.AbsoluteWebRoot));
  82. writer.WriteElementString(
  83. "lastmod", DateTime.Now.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
  84. writer.WriteElementString("changefreq", "daily");
  85. writer.WriteEndElement();
  86. }
  87. writer.WriteEndElement();
  88. }
  89. context.Response.ContentType = "text/xml";
  90. }
  91. #endregion
  92. #endregion
  93. }
  94. }