今天这篇文章主要向大家讲述一下Blog系统中应用最多的,具有Web2.0特性的,也是一种标准化的——Pingback&Trackback。分析一下BlogEngine.Net提供的比较全面的Pingback&Trackback支持。本文内容相对来说比较独立,如果您对整个系列感兴趣请参照这里。
Pingback&Trackback参考
简单地说,Pingback&Trackback是博客在链接、引用其他博客内容时通知对方博客的一种自动机制,告知对方链接位置及可能包含一定的摘要文本(视情况而定)。这样带来的好处是显而易见的,对Pingback&Trackback发送者来说,避免了手动通知对方的麻烦;对Pingback&Trackback接收者而言,则可以随时掌握自己的某篇文章被什么人链接及具体链接信息,等等。例如:
博客A发布了一篇文章;博客B看到后,发布评论、引用、介绍博客A上该篇文章的贴子;但此时博客A是无从知道博客B上对自己文章的引用情况的,要让博客A知道,博客B须:在博客A上留言,介绍自己对该篇文章的看法,比如说“在我的博客发布了一篇文章,也讨论这个问题——”,并附上相应页面的链接;向博客A发送E-mail,说明自己对该篇文章的进一步讨论,同时,也许希望博客A能在页面上放上自己的链接,以帮助对这一话题有兴趣的用户能找到自己的博客;Pingback&Trackback实现的功能便是将第三个步骤自动化了,再也不需这么一个繁琐的过程,即:博客A发布了一篇文章;博客B看到后,发布评论、引用、介绍博客A上该篇文章的贴子;在文章发布时,博客B即自动向博客A发送Pingback&Trackback;博客A收到相应的Pingback&Trackback,如果未设置人工审核,相应的内容即会出现在博客A该文的留言中,如果设置了人工审核,则出现在博客A的审核列表中,博客A认为不是spam后将其发布在留言中。
BlogEngine.Net中的Pingback&Trackback实现
BlogEngine.Net中的Pingback&Trackback的实现主要涉及到以下几组文件:
1.Web站点App_Code/Extensions中:SendPings.cs
2.BlogEngine.Core的Ping中:PingService.cs、Manager.cs、Pingback.cs、Trackback.cs
3.BlogEngine.Core的Web/HttpHandlers中:PingbackHandler.cs、TrackbackHandler.cs
从BlogEngine.Net中的Pingback&Trackback的具体实现来看,Pingback和Trackback的区别只在于数据的交换格式上,Pingback和PingService主要是使用标准的XMLRPC来完成数据的交换(上一篇文章中有部分涉及),而Trackback主要的request是使用类似的下面的格式完成:title={0}&url={1}&excerpt={2}&blog_name={3},response同样是使用XML传输
但是它们要实现的功能是类似的。
实际上SendPings类是BlogEngine.Net的一个Extension(类具有Extension特性,这个后面会有专门的一篇文章来讲解),它监听了Page.Saved和Post.Saved:
2 /// Hooks up an event handler to the Post.Saved event.
3 /// </summary>
4 static SendPings()
5 {
6 Post.Saved += new EventHandler < SavedEventArgs > (Post_Saved);
7 Page.Saved += new EventHandler < SavedEventArgs > (Post_Saved);
8 }
这里再次证明了BusinessBase这种状态维护方式的优越性。
通过代码我们可以看出当Page或Post被保存时会向SendPings发出通知,之后SendPings会从线程池中获得一个线程来向PingService发送Ping消息并使用Manager发送Pingback&Trackback消息。PingService这里主要是处理我们自己设置的Ping服务,对服务地址列表中的服务发送通知来说明某篇文章已经更新了。Manager主要分析文章中的超链接部分,找到Trackback入口地址,然后向该地址发送Trackback的TrackbackMessage(这是一个消息的封装,注意它使用Trackback请求的格式重写了ToString方法),如果发送不成功再向这个地址发送Pingback消息。Pingback和Trackback这两个类主要完成了Pingback&Trackback消息格式的具体处理并执行发送之。
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
22
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
26
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
使用PingbackHandler和TrackbackHandler对外提供支持
同样PingbackHandler和TrackbackHandler两个HttpHandler主要是为了使BlogEngine.Net也具有接收Pingback&Trackback消息并处理的能力。它们都是从请求中获得数据,并将这些数据生成一个Comment对象并关联到相应的文章中,同样它们也提供了Received,Accepted,Rejected,Spammed四个静态(类)事件来对这一处理周期进行跟踪,外部可以对这些事件提供相应的处理程序来达到扩展的目的。请大家注意,在Web站点中的Post.aspx中有下面一段代码:
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
<rdf:Description rdf:about="<%=Post.AbsoluteLink %>" dc:identifier="<%=Post.AbsoluteLink %>" dc:title="<%=Post.Title %>" trackback:ping="<%=Post.TrackbackLink %>" />
</rdf:RDF>
-->
这段代码被使用Html注释掉了,生成的Html为如下格式:
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
<rdf:Description rdf:about="http://localhost/BlogEngine.Web/post/2008/06/28/Welcome-to-BlogEngineNET-1-4.aspx" dc:identifier="http://localhost/BlogEngine.Web/post/2008/06/28/Welcome-to-BlogEngineNET-1-4.aspx" dc:title="Welcome to BlogEngine.NET 1.4.5" trackback:ping="http://localhost/BlogEngine.Web/trackback.axd?id=c3b491e5-59ac-4f6a-81e5-27e971b903ed" />
</rdf:RDF>
-->
http://localhost/BlogEngine.Web/trackback.axd?id=c3b491e5-59ac-4f6a-81e5-27e971b903ed实际上就是这篇文章的Trackback地址。
当然这个Trackback地址一般可以由用户自己指定,例如在博客园发文章时我们就可以自己输入一个。
参考文章
2.Trackback, Pingback , Backlink与博客
有些东西只有成为标准以后才会发光。
上一篇:BlogEngine.Net架构与源代码分析系列part6:开放API——MetaWeblog与BlogImporter
下一篇:BlogEngine.Net架构与源代码分析系列part8:扩展——DataStore分析
【来源】: http://thriving-country.cnblogs.com/
本文版权归作者和博客园共同所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。