網站地圖的作用是讓搜索引擎盡快的,更多的收錄網站的各個網頁。
這裡我們首先要明白一個基本的原理,搜索引擎的爬行方式。整個互聯網就像一張縱橫交錯的網:網的各個節點就是各個網頁,而各個網頁之間通過url相互連接。蜘蛛可以從一個網頁出發,通過該網頁上的url,爬到另一個網頁;再通過另一個網頁上的url,再爬到更多的網頁,以此類推。但如果是一個新發佈的網站,可能就沒有其他url指向它,那麼它就永遠不會被爬到(收錄)。為瞭解決這個問題,新站可以自己主動向搜索引擎提交url,申請蜘蛛前來抓取(Google申請網址:),但申請時一般隻會提交一個主頁的url。
為瞭讓所有的url(尤其是動態生成的)都能被蜘蛛快捷便利的檢索到,我們就需要提供一個全面完整、架構清晰和更新及時的網站地圖。
和處理重復內容的robots.txt文件,我們通過.ashx文件來生成一個基於sitemaps.org的xml格式的網站地圖。網站地圖生成之後,我們就可以向Google等搜索引擎提交。大量的文章證實,提交網站地圖將極大的提高網站的收錄速度和深度。其他幾乎所有的SEO方法,都有可能效果難以證實、失效甚至帶來副作用,但提交網站地圖除外!
Linq to XML為我們帶來瞭近乎完美的操作體驗。
<%@WebHandlerLanguage=C#Class=website%> usingSystem; publicclasswebsite:IHttpHandler{ publicvoidProcessRequest(HttpContextcontext){ context.Response.ContentType=text/xml; //文件的聲明信息,第第三個參數standalone的值yes表示這個XML文檔是自包含的(self-contained)而不依賴於外部所定義的一個DTD. //XML文件的命名空間 stringfixedUrl=/article; //循環取出數據,轉換成XML節點 wholeUrl=string.Format({0}?id={1}catelog={2},fixedUrl,item.ID,item.Catelog); url.Add(loc,lastmod,changefreq,priority); siteMap.Add(url); //最後輸出整個xml文件 publicboolIsReusable{ } |
同樣還將使用到xml技術的還有RSS
<%@WebHandlerLanguage=C#Class=rss%> usingSystem; publicclassrss:IHttpHandler{ publicvoidProcessRequest(HttpContextcontext){ context.Response.Write(<?xmlversion=\1.0\encoding=\UTF-8\?>); XElementrssFeed=newXElement(rss,newXAttribute(version,2.0)); stringfixedUrl=/article; XElementchannel=newXElement(channel, foreach(vararticleinArticles.GetArticles()) XElementtitle=newXElement(title,article.Title); wholeUrl=string.Format({0}?id={1}catelog={2},fixedUrl,article.ID,article.Catelog); XElementdescription=newXElement(description,article.Description); XElementpubDate=newXElement(pubDate,article.LastMod.ToString()); item.Add(title,link,description,pubDate); channel.Add(item); rssFeed.Add(channel); context.Response.Write(rssFeed); } publicboolIsReusable{ } |
模擬數據
usingSystem; usingSystem.Data; usingSystem.Configuration; usingSystem.Linq; usingSystem.Web; usingSystem.Web.Security; usingSystem.Web.UI; usingSystem.Web.UI.HtmlControls; usingSystem.Web.UI.WebControls; usingSystem.Web.UI.WebControls.WebParts; usingSystem.Xml.Linq; usingSystem.Web.UI.MobileControls; usingSystem.Collections.Generic; ///<summary> publicstaticList<Article>GetArticles() } publicclassArticle publicArticle(intid,stringcatelog,DateTimelastMod,Freqfrequency,doubleweight,stringtitle,stringdescription) publicenumFreq |
作者:自由飛 原文鏈接
參閱自由飛其他的文章
ASP.NET的SEO:HTTP報頭狀態碼內容重定向
asp.net的優化服務器控件背後友好的Html和JS
ASP.NET的SEO:使用.ashx文件排除重復內容