ASP.NET的SEO Linq to XML—網站地圖和RSS Feed

網站地圖的作用是讓搜索引擎盡快的,更多的收錄網站的各個網頁。

這裡我們首先要明白一個基本的原理,搜索引擎的爬行方式。整個互聯網就像一張縱橫交錯的網:網的各個節點就是各個網頁,而各個網頁之間通過url相互連接。蜘蛛可以從一個網頁出發,通過該網頁上的url,爬到另一個網頁;再通過另一個網頁上的url,再爬到更多的網頁,以此類推。但如果是一個新發佈的網站,可能就沒有其他url指向它,那麼它就永遠不會被爬到(收錄)。為瞭解決這個問題,新站可以自己主動向搜索引擎提交url,申請蜘蛛前來抓取(Google申請網址:),但申請時一般隻會提交一個主頁的url。

為瞭讓所有的url(尤其是動態生成的)都能被蜘蛛快捷便利的檢索到,我們就需要提供一個全面完整、架構清晰和更新及時的網站地圖。

和處理重復內容的robots.txt文件,我們通過.ashx文件來生成一個基於sitemaps.org的xml格式的網站地圖。網站地圖生成之後,我們就可以向Google等搜索引擎提交。大量的文章證實,提交網站地圖將極大的提高網站的收錄速度和深度。其他幾乎所有的SEO方法,都有可能效果難以證實、失效甚至帶來副作用,但提交網站地圖除外!

Linq to XML為我們帶來瞭近乎完美的操作體驗。

<%@WebHandlerLanguage=C#Class=website%>

usingSystem;
usingSystem.Web;
usingSystem.Xml;
usingSystem.Xml.Linq;
usingSystem.Linq;

publicclasswebsite:IHttpHandler{

publicvoidProcessRequest(HttpContextcontext){

context.Response.ContentType=text/xml;

//文件的聲明信息,第第三個參數standalone的值yes表示這個XML文檔是自包含的(self-contained)而不依賴於外部所定義的一個DTD.
XDeclarationdeclaration=newXDeclaration(1.0,UTF-8,yes);
context.Response.Write(declaration);

//XML文件的命名空間
XNamespacens=/schemas/sitemap/0.84;
XElementsiteMap=newXElement(ns+urlset);

stringfixedUrl=/article;
stringwholeUrl=string.Empty;

//循環取出數據,轉換成XML節點
foreach(variteminArticles.GetArticles())
{
XElementurl=newXElement(url);

wholeUrl=string.Format({0}?id={1}catelog={2},fixedUrl,item.ID,item.Catelog);
XElementloc=newXElement(loc,wholeUrl);
XElementlastmod=newXElement(lastmod,item.LastMod.AddDays(-23).ToShortDateString());
XElementchangefreq=newXElement(changefreq,item.Frequency);
XElementpriority=newXElement(priority,item.Weight);

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

siteMap.Add(url);
}

//最後輸出整個xml文件
context.Response.Write(siteMap);
}

publicboolIsReusable{
get{
returnfalse;
}
}

}

同樣還將使用到xml技術的還有RSS

<%@WebHandlerLanguage=C#Class=rss%>

usingSystem;
usingSystem.Web;
usingSystem.Xml;
usingSystem.Xml.Linq;

publicclassrss:IHttpHandler{

publicvoidProcessRequest(HttpContextcontext){
context.Response.ContentType=text/xml;

context.Response.Write(<?xmlversion=\1.0\encoding=\UTF-8\?>);

XElementrssFeed=newXElement(rss,newXAttribute(version,2.0));

stringfixedUrl=/article;
stringwholeUrl=string.Empty;

XElementchannel=newXElement(channel,
newXElement(title,freeflying),
newXElement(link,fixedUrl),
newXElement(description,thewebsitefordreamflyingfreely),
newXElement(pubDate,DateTime.Now.ToString())
);

foreach(vararticleinArticles.GetArticles())
{
XElementitem=newXElement(item);

XElementtitle=newXElement(title,article.Title);

wholeUrl=string.Format({0}?id={1}catelog={2},fixedUrl,article.ID,article.Catelog);
XElementlink=newXElement(link,wholeUrl);

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{
get{
returnfalse;
}
}

}

模擬數據

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>
///SummarydescriptionforArticles
///</summary>
publicclassArticles
{
publicArticles()
{
//
//TODO:Addconstructorlogichere
//
}

publicstaticList<Article>GetArticles()
{
returnnewList<Article>(){
newArticle(234,blog,DateTime.Now.AddDays(-23),Freq.none,0.8,asp.netseo,articlesaboutSEOinasp.net),
newArticle(267,blog,DateTime.Now.AddDays(-245),Freq.daily,0.6,ado.netpro,aboutthedatasetusage),
newArticle(653,news,DateTime.Now.AddDays(-45),Freq.daily,1,CLRviaC#,notebookaboutthisbook)
};
}

}

publicclassArticle
{
publicintID;
publicstringCatelog;
publicDateTimeLastMod;
publicdoubleWeight;
publicFreqFrequency;
publicstringTitle;
publicstringDescription;

publicArticle(intid,stringcatelog,DateTimelastMod,Freqfrequency,doubleweight,stringtitle,stringdescription)
{
ID=id;
Catelog=catelog;
LastMod=lastMod;
Weight=weight;
Frequency=frequency;
Title=title;
Description=description;
}
}

publicenumFreq
{
none=1,
daily=2,
weekly=3,
}

作者:自由飛 原文鏈接

參閱自由飛其他的文章

ASP.NET的SEO:HTTP報頭狀態碼內容重定向

asp.net的優化服務器控件背後友好的Html和JS

ASP.NET的SEO:使用.ashx文件排除重復內容