赞助商
上一篇:ASP.NET 的SEO:HTTP报头状态码---内容重定向

网站地图的作用是让搜索引擎尽快的,更多的收录网站的各个网页。
   
这里我们首先要明白一个基本的原理,搜索引擎的爬行方式。整个互联网就像一张纵横交错的“网”:网的各个节点就是各个网页,而各个网页之间通过url相互连接。蜘蛛可以从一个网页出发,通过该网页上的url,爬到另一个网页;再通过另一个网页上的url,再爬到更多的网页……,以此类推。但如果是一个新发布的网站,可能就没有其他url指向它,那么它就永远不会被“爬到”(收录)。为了解决这个问题,新站可以自己主动向搜索引擎提交url,申请蜘蛛前来抓取(Google申请网址:),但申请时一般只会提交一个主页的 url。(文/自由飞

为了让所有的url(尤其是动态生成的)都能被蜘蛛快捷便利的检索到,我们就需要提供一个全面完整、架构清晰和更新及时的网站地图。(网站地图的更多信息)。

和处理重复内容的robots.txt文件,我们通过.ashx文件来生成一个基于sitemaps.org的 xml格式的网站地图。网站地图生成之后,我们就可以向Google等搜索引擎提交。大量的文章证实,提交网站地图将极大的提高网站的收录速度和深度。其他几乎所有的SEO方法,都有可能效果难以证实、失效甚至带来副作用,但提交网站地图除外!

Linq to XML为我们带来了近乎完美的操作体验。
WebSite
  1. <%@ WebHandler Language="C#" Class="website" %>

  2. using System;
  3. using System.Web;
  4. using System.Xml;
  5. using System.Xml.Linq;
  6. using System.Linq;

  7. public class website : IHttpHandler {
  8.    
  9.     public void ProcessRequest (HttpContext context) {

  10.         context.Response.ContentType = "text/xml";

  11.         //文件的声明信息,第第三个参数standalone的值yes 表示这个 XML 文档是自包含的(self-contained)而不依赖于外部所定义的一个 DTD.
  12.         XDeclaration declaration = new XDeclaration("1.0", "UTF-8", "yes");
  13.         context.Response.Write(declaration);
  14.        
  15.         //XML文件的命名空间
  16.         XNamespace ns = "http://www.google.com/schemas/sitemap/0.84";
  17.         XElement siteMap = new XElement(ns + "urlset");

  18.         string fixedUrl = "http://www.freeflying.com/article";
  19.         string wholeUrl = string.Empty;
  20.        
  21.         //循环取出数据,转换成XML节点
  22.         foreach (var item in Articles.GetArticles())
  23.         {
  24.             XElement url = new XElement("url");

  25.             wholeUrl = string.Format("{0}?id={1}&catelog={2}",fixedUrl,item.ID,item.Catelog);
  26.             XElement loc = new XElement("loc", wholeUrl);
  27.             XElement lastmod = new XElement("lastmod", item.LastMod.AddDays(-23).ToShortDateString());
  28.             XElement changefreq = new XElement("changefreq", item.Frequency);
  29.             XElement priority = new XElement("priority", item.Weight);

  30.             url.Add(loc, lastmod, changefreq, priority);

  31.             siteMap.Add(url);
  32.         }

  33.        
  34.        
  35.         //最后输出整个xml文件
  36.         context.Response.Write(siteMap);
  37.     }

  38.     public bool IsReusable {
  39.         get {
  40.             return false;
  41.         }
  42.     }

  43. }
复制代码
同样还将使用到xml技术的还有RSS
RSS
  1. <%@ WebHandler Language="C#" Class="rss" %>

  2. using System;
  3. using System.Web;
  4. using System.Xml;
  5. using System.Xml.Linq;


  6. public class rss : IHttpHandler {
  7.    
  8.     public void ProcessRequest (HttpContext context) {
  9.         context.Response.ContentType = "text/xml";

  10.         context.Response.Write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");

  11.         XElement rssFeed = new XElement("rss", new XAttribute("version","2.0"));

  12.         string fixedUrl = "http://www.freeflying.com/article";
  13.         string wholeUrl = string.Empty;

  14.         XElement channel = new XElement("channel",
  15.             new XElement("title", "freeflying"),
  16.             new XElement("link", fixedUrl),
  17.             new XElement("description","the website for dream flying freely"),
  18.             new XElement("pubDate",DateTime.Now.ToString())
  19.             );
  20.        
  21.        
  22.         foreach (var article in Articles.GetArticles())
  23.         {
  24.             XElement item = new XElement("item");

  25.             XElement title = new XElement("title", article.Title);

  26.             wholeUrl = string.Format("{0}?id={1}&catelog={2}", fixedUrl, article.ID, article.Catelog);
  27.             XElement link = new XElement("link", wholeUrl);

  28.             XElement description = new XElement("description", article.Description);

  29.             XElement pubDate = new XElement("pubDate", article.LastMod.ToString());

  30.             item.Add(title,link,description,pubDate);

  31.             channel.Add(item);
  32.         }

  33.         rssFeed.Add(channel);

  34.         context.Response.Write(rssFeed);

  35.     }

  36.     public bool IsReusable {
  37.         get {
  38.             return false;
  39.         }
  40.     }
  41.    

  42. }
复制代码
模拟数据
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.HtmlControls;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.WebControls.WebParts;
  11. using System.Xml.Linq;
  12. using System.Web.UI.MobileControls;
  13. using System.Collections.Generic;

  14. /// <summary>
  15. /// Summary description for Articles
  16. /// </summary>
  17. public class Articles
  18. {
  19.     public Articles()
  20.     {
  21.         //
  22.         // TODO: Add constructor logic here
  23.         //
  24.     }

  25.     public static List<Article> GetArticles()
  26.     {
  27.         return new List<Article>(){
  28.             new Article(234, "blog", DateTime.Now.AddDays(-23), Freq.none, 0.8, "asp.net seo", "articles about SEO in asp.net"),
  29.             new Article(267, "blog", DateTime.Now.AddDays(-245), Freq.daily, 0.6, "ado.net pro","about the dataset usage"),
  30.             new Article(653, "news", DateTime.Now.AddDays(-45), Freq.daily, 1,"CLR via C#","notebook about this book")
  31.         };
  32.     }


  33. }

  34. public class Article
  35. {
  36.     public int ID;
  37.     public string Catelog;
  38.     public DateTime LastMod;
  39.     public double Weight;
  40.     public Freq Frequency;
  41.     public string Title;
  42.     public string Description;

  43.     public Article(int id, string catelog, DateTime lastMod, Freq frequency, double weight, string title, string description)
  44.     {
  45.         ID = id;
  46.         Catelog = catelog;
  47.         LastMod = lastMod;
  48.         Weight = weight;
  49.         Frequency = frequency;
  50.         Title = title;
  51.         Description = description;
  52.     }
  53. }

  54. public enum Freq
  55. {
  56.     none = 1,
  57.     daily = 2,
  58.     weekly = 3,
  59. }
复制代码
下一篇:ASP.NET 的SEO:SEO Hack --- Html注入和Nofollow
赞助商
赞助商
TOP