2011-02-17 49 views
1

我的网站有动态内容,正在创建新链接。asp.net中的sitemaps.xml mvc

我的数据库有一个表,其中几乎包含所有添加的网址。

所以我的问题是如何importnt是我有sitemaps.xml,也有一个简单的方法来构建它,以便当新的链接生成时,我可以标记它到一个sitemap.xml文件的末尾?

回答

2

您可以使用LINQ to XML从数据库创建一个XML站点地图,然后从一个动作返回站点地图返回Content(document.ToString(), "text/xml")

2

抓取工具能够更快,更准确地抓取您的网站,这一点非常重要。

您可以创建一个控制器,说siteMapController和指数添加以下

public ActionResult Index() { 
     var xmlString = 
      "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\">"; 

     xmlString += 
       "<url>" + 
       "<loc>Your site</loc>" + 
       "<changefreq>weekly</changefreq>" + 
       "<priority>0.6</priority>" + 
      "</url>" + 
      "<url>" + 
        "<loc>Static Link of you site</loc>" + //you can add as many you want 
         "<changefreq>weekly</changefreq>" + 
         "<priority>0.6</priority>" + 
        "</url>" + 
        "<url>"; 


      //Dynamic links 
      xmlString += "<url>" + 
         "<loc>Link of new item"</loc>" + 
         "<lastmod>" + DateTime.Now.ToString("yyyy-MM-dd") + "</lastmod>" + 
         "<changefreq>daily</changefreq>" + 
         "<priority>0.8</priority>" + 
         "</url>"; 
     } 
     xmlString += "</urlset>"; 
     ViewData["siteMap"] = xmlString; 
     return View(); 
    } 

保存在服务器上的XML,并通过此链接

https://www.google.com/webmasters/tools/home?hl=en

希望帮助发布网站地图

+0

如果您的网站上有20,000个动态生成的网页,该怎么办?如果你将它们都包含在网站地图中,或者Google会忽略这么大的条目。另外,您通常应该避免通过字符串连接来构建XML。直接写入响应流或在最糟糕的情况下使用XML API或StringBuilder。 – 2011-02-17 11:05:04

+0

好吧,这会给我一个XML文件在http://www.mysite.com/sitemap/?问题是,当新创建的链接添加到数据库我必须重新渲染整个XML文件? – raklos 2011-02-17 11:08:03