极地银狐.NET

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
表告诉我说你不知道nopCommerce是什么。它是目前.NET中最流行的完全开源网上商城,由俄罗斯的团队在2008年开始立项一直开发到现在已经是3.3版本了。代码目前托管在codeplex上,有兴趣的同学可以猛戳他们的codeplex站点http://nopcommerce.codeplex.com/
 
目的和效果

大家造在目前的nopCommerce中已经有网站地图的功能,当你请求如下地址时,http://www.nopchina.com/SitemapSeo会显示XML的站点地图,但有个问题,这个是实时显示的。站点地图其实不用实时,这个你懂的,于是我们要做的是让它定时生成一个静态的XML文件就好,不需要实时生成以节约服务器资源。

做好以后放到插件中如下:

如何实现

制作一个nop插件,通过nopCommerce的计划任务来实现定时生成。插件项目如下:

代码可以在此下载:Sea.Plugin.Job.SitemapXML.zip,此处仅略为描述主要功能:

插件安装时向计划任务添加一条记录:

public override void Install()        {

            //install a schedule task

            var task = FindScheduledTask();

            if (task == null)

            {

                task = new ScheduleTask

                {

                    Name = "[SEA] sitemap.xml Generator",

                    //everyday

                    Seconds = 3600*24,

                    //Seconds = 60,//test

                    Type = "Sea.Plugin.Job.SitemapXML.SiteMapGenerationTask, Sea.Plugin.Job.SitemapXML",

                    Enabled = true,

                    StopOnError = false,

                };

                _scheduleTaskService.InsertTask(task);

            }

            SiteMapGenerationTask currentTask = new SiteMapGenerationTask(_sitemapGenerator,_webHelper);

            currentTask.Execute();

          base.Install();

        }

当时卸载的时候也要删除干净:

public override void Uninstall()        {

            //remove the task

            var task = FindScheduledTask();

            if (task != null)

                _scheduleTaskService.DeleteTask(task);

          base.Uninstall();

}

在计划任务的execute方法中,调用现有的SEO类生成地图:

public void Execute()

        {

            string xml = _sitemapGenerator.Generate();

            string filePath = _webHelper.MapPath("~/sitemap.xml");

            File.WriteAllText(filePath, xml);

        }

来数数这两个文件你要写多少行代码? :)

哦,别忘了把之前已有的Sitemap action给注释掉:

//SEO sitemap page

[NopHttpsRequirement(SslRequirement.No)]

public ActionResult SitemapSeo()

{

if (!_commonSettings.SitemapEnabled)

return RedirectToRoute("HomePage");

 

//string siteMap = _sitemapGenerator.Generate();

//return Content(siteMap, "text/xml");

return Redirect("/sitemap.xml");

}

posted on 2014-06-14 17:09  极地银狐.NET  阅读(2303)  评论(6编辑  收藏  举报