在BlogEngine.net的文章底部显示Google AdSense广告

by Jason 2008-09-06,05:11

申请到Google AdSense之后,就想在每篇文章末尾显示一个Google AdSense广告。尝试着在SiteMaster.ascx、PostView.ascx等文件中添加广告代码,效果都不是太满意,最后决定利用Blogengine.net的扩展(Extention)机制,在每篇文章的末尾自动添加Google AdSense广告。这样做的好处是不必在文章中手动添加广告代码,不会对数据库中的文章数据有任何影响,和Theme无关,同时显示效果也比较令人满意。本来应该在这个扩展中设置一个用于输入广告代码的参数,可惜Blogengine.net输入扩展参数的界面只支持单行字符,只好手动添加在文件中了。下面是这个扩展的源代码,复制后另存为PostAD.cs,用广告代码替换一下[ADCODE],然后把PostAD.cs复制到App_Code\Extensions目录下就可以用了。显示的效果见www.tiray.net
为了测试这个功能在本地和网站上反反复复试了好多次,但愿Google不会认为我是在作弊,删了我的Google AdSense ID。

using System;
using System.Data;
using System.Text.RegularExpressions;
using BlogEngine.Core;
using BlogEngine.Core.Web.Controls;
 
/// <summary>
/// Add Google AdSense Code to the bottom of post.
/// Written by Jaosn 2008-09-06 Visit www.tiray.net for details
/// </summary>
[Extension("Add Google AdSense Code to the bottom of post", "1.0", "<a href=\"http://www.tiray.net\">www.tiray.net</a>")]
public class PostAD
{
    static PostAD()
    {
        Post.Serving += new EventHandler<ServingEventArgs>(Post_Serving);
    }
 
    /// <summary>
    /// The event handler that is triggered every time a post is served to a client.
    /// </summary>
    private static void Post_Serving(object sender, ServingEventArgs e)
    {
        //Add Google AdSense only when a single post is served to a client
        if (e.Location == ServingLocation.SinglePost)
        {
            e.Body = e.Body +
"<br><div id=\"googlead\" style=\"text-align: center\">" + "\r\n" +
"[ADCODE]"+"\r\n"+
"</div><br>"+"\r\n";
 
 
        }
    }
}


第一个打分

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
标签:, , , ,
分类:.NET编程 | 博客技术

评论

添加评论




biuquote
  • 评论
  • 在线预览
Loading

Powered by BlogEngine.NET 1.4.5.10 Theme by Mads Kristensen
滇ICP备06001863号

我的软件

最新评论

Comment RSS

声明

  如非特别注明,本网站发布的所有文章、源代码及软件均为原创,其版权归www.tiray.net所有。如需转载或引用,请注明出处并通知作者。
  本网站建立于中华人民共和国境内,受中华人民共和国法律法规约束。请勿在本网站发表违反国家法律法规的言论。