在要求输入邮箱的文本域,请填写真实的邮件地址。非真实邮件地址,将收不到回复信息。

BlogEngine.NET-博易sitemap自动生成插件

DotNet 清风 445℃ 0评论

博易遵循 W3C 标准,具有极强的可扩展性、高性能、良好的 SEO 性能和非凡的用户体验。不仅如此,博易还拥有一批高质量的用户群,他们的支持使博易得以不断地发展。 博易不仅开源,而且免费。所有的技术支持全部都是无偿提供的!不过目前最新的BlogEngine.NET(博易)博客已经停止更新,现在主打了.net core的轻量级博客版本,叫Blogifier,接受有偿的主题定制等。不过无论是否收费,现在已经没有继续使用BlogEngine.NET(博易)这个博客了。之前使用过一段时间,自己也根据相关文档写了需要的一个插件,当文章发布后,产生(更新)根目录下的sitemap.xml。这里做个简单的记录。


using BlogEngine.Core.Web.Extensions;
using System;
using System.IO;
using System.Web.Hosting;
using System.Collections.Generic;
using BlogEngine.Core;
using BlogEngine.Core.Web.Controls;
using System.Web;
using System.Linq;
using System.Globalization;
using System.Xml;

namespace App_Code.Extensions
{


    /// <summary>
    /// After the release of the blog automatically generate a static xml sitemap file.
    /// Many search engines do not support the axd type address .
    /// </summary>
    [Extension("After the release of the blog automatically generate a static xml sitemap file.Many search engines do not support the axd type address .", "1.0", "skyfinder", 0, false)]
    public class SitemapFileXML
    {
        #region Constants and Fields

        private const string BaseFilename = "sitemap.xml";

        /// <summary>
        /// The sync root.
        /// </summary>
        private static readonly object SyncRoot = new object();


        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes static members of the  class.
        /// </summary>
        static SitemapFileXML()
        {
            Post.Saved+= OnWriter;
        }

        #endregion



        #region Methods

        /// <summary>
        /// This event generates a static sitemap.xml file at the root of the program.
        /// </summary>
        ///
        /// The sender.
        /// 
        ///
        /// The event arguments.
        /// 
        private static void OnWriter(object sender, EventArgs e)
        {
            if (sender == null)
            {
                return;
            }
            if (!ExtensionManager.ExtensionEnabled("SitemapFileXML"))
            {
                return;
            }
           var file = HostingEnvironment.ApplicationPhysicalPath+"\\"+BaseFilename;
            lock (SyncRoot)
            {
                try
                {
                    FileInfo fi = new FileInfo(file);
                    fi.Delete();

                    using (var writer = XmlWriter.Create(file))
                    {
                        writer.WriteStartElement("urlset", BlogConfig.SiteMapUrlSet);

                        // Posts
                        foreach (var post in Post.Posts.Where(post => post.IsVisibleToPublic))
                        {
                            writer.WriteStartElement("url");
                            writer.WriteElementString("loc", post.AbsoluteLink.AbsoluteUri.ToString());
                            writer.WriteElementString(
                                "lastmod", post.DateModified.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
                            writer.WriteElementString("changefreq", "monthly");
                            writer.WriteEndElement();
                        }

                        // Pages
                        foreach (var page in Page.Pages.Where(page => page.IsVisibleToPublic))
                        {
                            writer.WriteStartElement("url");
                            writer.WriteElementString("loc", page.AbsoluteLink.AbsoluteUri);
                            writer.WriteElementString(
                                "lastmod", page.DateModified.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
                            writer.WriteElementString("changefreq", "monthly");
                            writer.WriteEndElement();
                        }

                        // Removed for SEO reasons
                        //// Archive
                        // writer.WriteStartElement("url");
                        // writer.WriteElementString("loc", Utils.AbsoluteWebRoot.ToString() + "archive.aspx");
                        // writer.WriteElementString("lastmod", DateTime.Now.ToString("yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture));
                        // writer.WriteElementString("changefreq", "daily");
                        // writer.WriteEndElement();

                        // Contact
                        writer.WriteStartElement("url");
                        writer.WriteElementString("loc", $"{Utils.AbsoluteWebRoot}contact.aspx");
                        writer.WriteElementString("lastmod", DateTime.Now.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
                        writer.WriteElementString("changefreq", "monthly");
                        writer.WriteEndElement();

                        // Blog
                        if (Page.GetFrontPage() != null)
                        {
                            writer.WriteStartElement("url");
                            writer.WriteElementString("loc", $"{Utils.AbsoluteWebRoot}blog.aspx");
                            writer.WriteElementString(
                                "lastmod", DateTime.Now.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
                            writer.WriteElementString("changefreq", "daily");
                            writer.WriteEndElement();
                        }

                        writer.WriteEndElement();
                    }
                }
                catch(Exception ex)
                {
                    // Absorb the error.
                    Utils.Log($"Error write xml website SitemapFileXML: {ex.Message}");
                }
            }
        }

        #endregion
    }
}

 

 

 

转载请注明:清风博客 » BlogEngine.NET-博易sitemap自动生成插件

喜欢 (0)or分享 (0)
支付宝扫码打赏 微信打赏
发表我的评论
取消评论

CAPTCHA Image
Reload Image
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址