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