2013-0510

ASP.NET MVC动态生成xml格式的SiteMap

作者: momy 分类: 编程开发 0 Comment »
摘要:在SEO搜索优化中都有要求提交SiteMap的,例如google,百度都有这个功能。ASP.NET MVC动态生成xml格式的SiteMap和SiteMapIndex。

在SEO搜索优化中都有要求提交SiteMap的,例如google,百度都有这个功能。具体意义不再本文讨论中。


几个重要的问题

  1. 生成文章类动态sitemap.xml文件

  2. 生成sitemapindex.xml文件

  3. 何时调整以上两个文件做到时效性


第一步生成以上两个xml文件,以下代码放于HomeController文件中,以方便获取域名信息

private void CreateSiteMapForArticles()
        {
            string path = Server.MapPath("~/") + @"\SiteMap\";
            if (!System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
            }
            XElement siteMapForArticles = new XElement("urlset");
            //这里开始循环写数据
            var articles = db.Articles.OrderByDescending(d => d.PubTime).ToList();
            foreach (var article in articles)
            {
                siteMapForArticles.Add(new XElement("url",
                    new XElement("loc", Request.Url.Scheme + "://" + Request.Url.Host + (Request.Url.Port == 80 ? "" : ":" + Request.Url.Port) + "/Article/Detail/" + article.ID),
                    new XElement("lastmod", article.PubTime),
                    new XElement("changefreq", "weekly"),
                    new XElement("priority", 1.0)
                    ));
            }
            siteMapForArticles.Save(path + "sitemapforarticles.xml");
        }
        private void CreateSiteMap()
        {
            string path = Server.MapPath("~/") + @"\SiteMap\";
            if (!System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
            }
            string[] files = System.IO.Directory.GetFiles(path);
            XNamespace xnamespace = "http://www.sitemaps.org/schemas/sitemap/0.9";
            XElement siteMap = new XElement(xnamespace+"sitemapindex");
            foreach (var file in files)
            {
                System.IO.FileInfo fileInfo = new System.IO.FileInfo(file);
                siteMap.Add(new XElement(xnamespace+"sitemap",
                    new XElement(xnamespace+"loc", Request.Url.Scheme + "://" + Request.Url.Host + (Request.Url.Port == 80 ? "" : ":" + Request.Url.Port) + "/SiteMap/" + fileInfo.Name),
                    new XElement(xnamespace+"lastmod", fileInfo.LastAccessTime)
                    ));
            }
            siteMap.Save(Server.MapPath("~/") + @"/sitemapindex.xml");
        }

以上代码生成了文章类sitemap和sitemap的索引文件,百度提交的时候我们只需要提交sitemapindex.xml文件,如果需要生成其他文件请保存与sitemap文件夹内。


何时来更新以上两个文件?

添加文章后更新,做到即时性,我们用数据库缓存依赖实现,以下代码放于HomeController里边的Index里,实现缓存依赖:

//是否更新SiteMap
            System.Web.Caching.Cache cache = HttpRuntime.Cache;
            object articlesIsChange = cache.Get("articles_changed");
            if (articlesIsChange == null)
            {
                SqlCacheDependencyAdmin.EnableNotifications(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
                SqlCacheDependencyAdmin.EnableTableForNotifications(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString, "Article");
                SqlCacheDependency scd = new SqlCacheDependency("SiteInfoSqlDependency", "Article");
                cache.Insert("articles_changed", "true", scd, DateTime.Now.AddHours(24), Cache.NoSlidingExpiration);
                //更新SiteMap
                CreateSiteMapForArticles();
                CreateSiteMap();
            }


具体数据库缓存依赖请参考:http://www.yn-s.com/News/Details/20

标签: MVC SiteMap SiteMapIndex Xml 阅读: 16328
上一篇: WPF绑定类属性 - 13538次
下一篇: 那些强悍的PHP一句话后门 - 12897次

向右滑动解锁留言